POSIX regex in C is not working
I want to match every thing in between two words GET and HTTP. I tried every开发者_如何学编程 thing I know. But it is not working. Any help appreciated. The pattern GET.*HTTP should match GET www.google.com HTTP.
Here is the code
Headers:
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Main:
int main(int argc, char *argv[]) {
regex_t regex;
int reti;
char msgbuf[100];
regmatch_t pmatch[1];
/* Compile regular expression */
reti = regcomp(®ex, "GET.*HTTP", REG_EXTENDED);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
/* Execute regular expression */
reti = regexec(®ex, argv[1], 1, pmatch, 0);
if (!reti) {
puts("Match");
char *match = strndup(argv[1] + pmatch[0].rm_so, pmatch[0].rm_eo - pmatch[0].rm_so);
printf("%s\n",match);
} else if (reti == REG_NOMATCH) {
puts("No match");
} else {
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
/* Free compiled regular expression if you want to use the regex_t again */
regfree(®ex);
return 0;
}
Is there any thing I'm doing wrong here.
Looks like my comment was the answer...
The problem is that the command line argument was chopped into 3 strings, making argv[1]
pointing to GET
only.
To pass the entire string to the program, you must use double quotes:
$ ./regex GET www.google.com HTTP
No match
$ ./regex "GET www.google.com HTTP"
Match
GET www.google.com HTTP
$
精彩评论