Jump to content
Sign in to follow this  
Zic0

regex problem - finner ikke bokstav engang

Recommended Posts

Hallo

Har ett problem med regex jeg ikke skjønner.

 

#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <string>
#include "boost/regex.hpp"

using namespace std;
using namespace boost;
SOCKET s;


bool ConnectToHost(char* ip, int port){
    
    WSADATA wsadata;
    
    if( WSAStartup(0x0202, &wsadata) ){
        printf("Kunne ikke innstalere WSA\n");
        return false;
    }
    
    if( wsadata.wVersion != 0x0202){
        printf("Winsock krever ver 2\n");
        return false;
    }
    
    SOCKADDR_IN target;
    target.sin_family = AF_INET;
    target.sin_port = htons(port);
    target.sin_addr.s_addr = inet_addr(ip);
    
    s = socket(AF_INET, SOCK_STREAM, 0);
    
    if(s == INVALID_SOCKET){
         printf("Kunne ikke lage socket'en\n");
         return false;
    }
    
    if( connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR){
        return false;
    }
    
    return true;
}

void PrintData(string nick, string name){
    
   nick = "NICK " + nick + "\n";
   name = "USER " + name + " " + name + "_ homelien: " + name + "\n";
   char* cmd;
    
   char buf[512];
    if(s){
          
          int iResult;
          send(s, nick.data(), nick.size(), 0);
          send(s, name.data(), name.size(), 0);
          
          printf(nick.data());
          printf(name.data());
          
          regex expression("Z");
          cmatch result;
          do {
              iResult = recv(s, buf, sizeof(buf) - 1, 0);
              
              if( iResult > 0){
                  
                 if( regex_match(buf, result, expression) ){
                      cmd = "JOIN #c++.no\n";
                      send(s, cmd, sizeof(cmd), 0);
                      printf("\nJoiner #c++.no\n");
                  }
               
                  printf(buf);
              }
              
              else if(iResult == 0){
                   printf("Tilkobling avsluttes");
              }
              
              else {
                   printf("Error med å motta data");
              }
              
          } while(iResult > 0);
          
    } else {
      printf("Socket'en eksisterer ikke");
    }
}

int main(int argc, char *argv[])
{
   
   if( ConnectToHost("195.159.0.90", 6667) == true){
       printf("Tilkoblet\n");
       PrintData("Zico", "Zicohost");
   } else {
     printf("Kunne ikke koble til\n");
   }
   
   if(s)
   closesocket(s);
   WSACleanup();
   
   system("PAUSE");
   return EXIT_SUCCESS;
}

 

Problemet er at jeg ikke skjønner hvordan den IKKE greier å finne Z i bufferen. Det finner jeg meg ikke mulig i ettersom den printer ut "Zico" på hver linje. Håper noen kan hjelpe.

Share this post


Link to post

Følgende kode gjør deg kanskje litt klokere?

 

#include <iostream>
#include <string>
#include <boost/regex.hpp>

using namespace std;
using namespace boost;

int main()
{
   string str("Zico");
   regex expA("Z");
   regex expB("Z.*");
   cmatch result;

   if(regex_match(str.c_str(), result, expA))
   {
       cout << "A: " << str << endl;
   }

   if(regex_match(str.c_str(), result, expB))
   {
       cout << "B: " << str << endl;
   }
}

 

Ta ellers en titt på:

 

http://boost.org/libs/regex/doc/

 

http://regular-expressions.info/

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...