I have this pattern [-]{23}[ ]*Page[ ]*[0-9]*[-]{23}to extract the page number from an string like ----------------------- Page 1----------------------- it works fine using javascript regex implementation:
var s = "----------------------- Page 1-----------------------";
alert( s.match(/[-]{23}[ ]*Page[ ]*[0-9]*[-]{23}/) != null);
match() function returns the matched string value or null if pattern does not match with string. The above code show true
my C code:
#include <assert.h>
#include <sys/types.h>
#include <regex.h>
//...
regex_t reg;
regmatch_t match;
char * line = "----------------------- Page 1-----------------------";
regcomp(®,
"[-]{23}[ ]*Page[ ]*[0-9]*[-]{23}",
REG_ICASE /* Don't differentiate case */
);
int r = regexec(®,
line, /* line to match */
1, /* size of captures */
&match,
0);
if( r == 0) { printf("Match!"); } else { printf("NO match!"); }
the if-statement above print NO match! I have no idea how to fix this. Thanks in advance.
For the regex library to recognize the full regular expression, use REG_EXTENDED in the regcomp flag.
it is possible to use
groups?
Do you mean capturing groups? Like this?
#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
int main(void) {
int r;
regex_t reg;
regmatch_t match[2];
char *line = "----------------------- Page 1-----------------------";
regcomp(®, "[-]{23}[ ]*Page[ ]*([0-9]*)[-]{23}", REG_ICASE | REG_EXTENDED);
/* ^------^ capture page number */
r = regexec(®, line, 2, match, 0);
if (r == 0) {
printf("Match!\n");
printf("0: [%.*s]\n", match[0].rm_eo - match[0].rm_so, line + match[0].rm_so);
printf("1: [%.*s]\n", match[1].rm_eo - match[1].rm_so, line + match[1].rm_so);
} else {
printf("NO match!\n");
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With