J
Jonas
I got a string from which I want to extract some info. The string has a
format like this "$MyINFO $ALL %s %s$ $%s$%s$%s$|" ie "$MyINFO $ALL
[Sunet]smurf hmm$ $LAN(T3)[email protected]$85899345920$|" for doing this I use
the The GNU C Library regular expression library.
My matchstring looks like this "$MyINFO $ALL \\(.*\\) \\(.*\\)$
$\\(.*\\)$\\(.*\\)$\\(.*\\)$|" it works fine for the 4 strings but it's
unable to match the last one which it seems to think starts from the first
space and ends at '|'. I'm a little unsure how the escapes characters works
but I think I got them right, the manual doesnt say much about them (
http://www.gnu.org/manual/glibc-2.2.5/html_mono/libc.html#Regular Expressi
ons ).
thx for your help
/*recives a like $MyINFO $ALL [Sunet]smurf hmm$
$LAN(T3)[email protected]$85899345920$|*/
void process_myinfo( char* cmd ){
regex_t comp_reg;
regmatch_t match[7];
char* sp;
int n;
printf("got: %s\n", cmd);
n = regcomp( &comp_reg, "$MyINFO $ALL \\(.*\\) \\(.*\\)$
$\\(.*\\)$\\(.*\\)$\\(.*\\)$|", 0 );
if( n!=0 ){
printf( "regcomp failed:\n" );
regfree( &comp_reg );
return;
}
n = regexec( &comp_reg, cmd, 5, match, 0 );
if( n==0 ){
printf( "string is matching reg exp\n");
sp = substring( cmd, match[1].rm_so, match[1].rm_eo );
if( sp!=NULL ){
printf("nick: %s\n", sp);
}
free(sp);
sp = substring( cmd, match[2].rm_so, match[2].rm_eo );
if( sp!=NULL ){
printf("desc: %s\n", sp);
}
free(sp);
sp = substring( cmd, match[3].rm_so, match[3].rm_eo );
if( sp!=NULL ){
printf("connection: %s\n", sp);
}
free(sp);
sp = substring( cmd, match[4].rm_so, match[4].rm_eo );
if( sp!=NULL ){
printf("mail: %s\n", sp);
}
free(sp);
printf("start: %d stop: %d\n", match[5].rm_so, match[5].rm_eo);
sp = substring( cmd, match[5].rm_so, match[5].rm_eo );
if( sp!=NULL ){
printf("share: %s\n", sp);
}
free(sp);
fflush(stdout);
}
else{
printf("no match\n");
}
regfree( &comp_reg );
}
outputs:
got: $MyINFO $ALL [Sunet]smurf hmm$ $LAN(T3)[email protected]$85899345920$|
string is matching reg exp
nick: [Sunet]smurf
desc: hmm
connection: LAN(T3)
mail: (e-mail address removed)
start: 6 stop: 70
format like this "$MyINFO $ALL %s %s$ $%s$%s$%s$|" ie "$MyINFO $ALL
[Sunet]smurf hmm$ $LAN(T3)[email protected]$85899345920$|" for doing this I use
the The GNU C Library regular expression library.
My matchstring looks like this "$MyINFO $ALL \\(.*\\) \\(.*\\)$
$\\(.*\\)$\\(.*\\)$\\(.*\\)$|" it works fine for the 4 strings but it's
unable to match the last one which it seems to think starts from the first
space and ends at '|'. I'm a little unsure how the escapes characters works
but I think I got them right, the manual doesnt say much about them (
http://www.gnu.org/manual/glibc-2.2.5/html_mono/libc.html#Regular Expressi
ons ).
thx for your help
/*recives a like $MyINFO $ALL [Sunet]smurf hmm$
$LAN(T3)[email protected]$85899345920$|*/
void process_myinfo( char* cmd ){
regex_t comp_reg;
regmatch_t match[7];
char* sp;
int n;
printf("got: %s\n", cmd);
n = regcomp( &comp_reg, "$MyINFO $ALL \\(.*\\) \\(.*\\)$
$\\(.*\\)$\\(.*\\)$\\(.*\\)$|", 0 );
if( n!=0 ){
printf( "regcomp failed:\n" );
regfree( &comp_reg );
return;
}
n = regexec( &comp_reg, cmd, 5, match, 0 );
if( n==0 ){
printf( "string is matching reg exp\n");
sp = substring( cmd, match[1].rm_so, match[1].rm_eo );
if( sp!=NULL ){
printf("nick: %s\n", sp);
}
free(sp);
sp = substring( cmd, match[2].rm_so, match[2].rm_eo );
if( sp!=NULL ){
printf("desc: %s\n", sp);
}
free(sp);
sp = substring( cmd, match[3].rm_so, match[3].rm_eo );
if( sp!=NULL ){
printf("connection: %s\n", sp);
}
free(sp);
sp = substring( cmd, match[4].rm_so, match[4].rm_eo );
if( sp!=NULL ){
printf("mail: %s\n", sp);
}
free(sp);
printf("start: %d stop: %d\n", match[5].rm_so, match[5].rm_eo);
sp = substring( cmd, match[5].rm_so, match[5].rm_eo );
if( sp!=NULL ){
printf("share: %s\n", sp);
}
free(sp);
fflush(stdout);
}
else{
printf("no match\n");
}
regfree( &comp_reg );
}
outputs:
got: $MyINFO $ALL [Sunet]smurf hmm$ $LAN(T3)[email protected]$85899345920$|
string is matching reg exp
nick: [Sunet]smurf
desc: hmm
connection: LAN(T3)
mail: (e-mail address removed)
start: 6 stop: 70