I’ve been struggling with regular expressions in C (just /usr/include/regex.h).
I have (let’s say) hundreds of regexps and one of them can match input string.
Currently I’m doing it (generating it actually) like this: hundreds of do-while with match inside, break if not matching and going to another. One by one:
do {
if ( regex_match(str, my_regex1) != MY_REGEX_SUCCESS ) DO_FAIL; //break
...
if ( sscanf(str, " %d.%d.%d.%d / %d ", &___ip1, &___ip2, &___ip3, &___ip4, &___pref) != 5 ) DO_FAIL; //break
...
} while (0);
do {
if ( regex_match(str, my_regex2) != MY_REGEX_SUCCESS ) DO_FAIL; //break
...
...
} while (0);
do {
if ( regex_match(str, my_regex3) != MY_REGEX_SUCCESS ) DO_FAIL; //break
...
...
} while (0);
What I’d like to have is something like:
const char * match1 = "^([[:space:]]*)([$]([._a-zA-Z0-9-]{0,118})?[._a-zA-Z0-9])([[:space:]]*)$";
const char * match2 = "^([[:space:]]*)(target|origin)([[:space:]]*):([[:space:]]*)([$]([._a-zA-Z0-9-]{0,118})?[._a-zA-Z0-9])([[:space:]]*):([[:space:]]*)\\*([[:space:]]*)$";
const char * match3 = "^([[:space:]]*)(target|origin)([[:space:]]*):([[:space:]]*)([$]([._a-zA-Z0-9-]{0,118})?[._a-zA-Z0-9])([[:space:]]*)/([[:space:]]*)(([0-2]?[0-9])|(3[0-2]))([[:space:]]*):([[:space:]]*)(([1-9][0-9]{0,3})|([1-5][0-9]{4})|(6[0-4][0-9]{3})|(65[0-4][0-9]{2})|(655[0-2][0-9])|(6553[0-5]))([[:space:]]*)$";
char * my_match;
asprintf(&my_match, "(%s)|(%s)|(%s)", match1, match2, match3);
int num_gr = give_me_number_of_regex_group(str, my_match)
switch (num_gr) {
...
}
and don’t have an idea how to do that…
Any suggestions?
Thanks!
I assume your
regex_matchis some combination ofregcompandregexec. To enable grouping, you need to callregcompwith theREG_EXTENDEDflag, but without theREG_NOSUBflag (in the third argument).Then allocate space for the groups. The number of groups is stored in
compiled.re_nsub. Pass this number toregexec:Now, the first invalid group is the one with a -1 value in both its
rm_soandrm_eofields:nmatchedis the number of parenthesized subexpressions (groups) matched. Add your own error checking.