G
Gregory Toomey
Apologies if this is somehat offtopic.
Can somebody point to a simple example of matching subexpressions? I've read
http://www.gnu.org/software/libc/manual/html_node/Matching-POSIX-Regexps.html
Using the code below I can match "product_(.*)htm" to "product_abcd.htm".
What I get returned is "product_abcd.htm" but I want "abcd" (like the $1
matching variable in Perl). Is there an API for this?
gtoomey
------------
Return first substring mating the regex:
char *matchre ( char *pattern, char *string) {
char *p;
char *match;
int retval;
regex_t re;
char buf[BUFSIZE];
regmatch_t pmatch[100];
int status;
char *ps;
int eflag;
setlocale(LC_ALL, "");
if((status = regcomp( &re, pattern, REG_EXTENDED))!= 0){
regerror(status, &re, buf, 120);
exit(2);
}
ps = string;
eflag = 0;
match=(char *)malloc(1000);
if (status = regexec( &re, ps, 1, pmatch, eflag)==0){
/*printf("match found at: %d-%d, string=%s\n",
pmatch[0].rm_so, pmatch[0].rm_eo, ps
+pmatch[0].rm_so);*/
match=(char *)malloc(1000);
strcpy(match,ps +pmatch[0].rm_so);
/* return first match */
return match;
}
return "";
}
Can somebody point to a simple example of matching subexpressions? I've read
http://www.gnu.org/software/libc/manual/html_node/Matching-POSIX-Regexps.html
Using the code below I can match "product_(.*)htm" to "product_abcd.htm".
What I get returned is "product_abcd.htm" but I want "abcd" (like the $1
matching variable in Perl). Is there an API for this?
gtoomey
------------
Return first substring mating the regex:
char *matchre ( char *pattern, char *string) {
char *p;
char *match;
int retval;
regex_t re;
char buf[BUFSIZE];
regmatch_t pmatch[100];
int status;
char *ps;
int eflag;
setlocale(LC_ALL, "");
if((status = regcomp( &re, pattern, REG_EXTENDED))!= 0){
regerror(status, &re, buf, 120);
exit(2);
}
ps = string;
eflag = 0;
match=(char *)malloc(1000);
if (status = regexec( &re, ps, 1, pmatch, eflag)==0){
/*printf("match found at: %d-%d, string=%s\n",
pmatch[0].rm_so, pmatch[0].rm_eo, ps
+pmatch[0].rm_so);*/
match=(char *)malloc(1000);
strcpy(match,ps +pmatch[0].rm_so);
/* return first match */
return match;
}
return "";
}