/* * 01-26-2013 from *http://stackoverflow.com/questions/1085083/regular-expressions-in-c-examples * * Revised so it can recognize a URL in a stream of characters (a text) * This example examines one match (first one) only. If a pice of text * contains multiple matches, one has to extract them all. */ #include #include #include #include int main(int argc, char*argv[]) { regex_t regex; regmatch_t matched[8]; int nmatch = 1; int len; int reti; char msgbuf[128]; char * url_pattern = "anchor text"; // char * url = "anchor"; // char * url = "anchor "; // char * url = "anchor"; char * url = "some other text anchore after"; /* Compile regular expression */ reti = regcomp(®ex, url_pattern, REG_ICASE); if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); } /* Execute regular expression */ reti = regexec(®ex, url, nmatch, matched, 0); if( !reti ) { puts("Match"); len = matched[0].rm_eo - matched[0].rm_so; printf("b=%d e=%d\n", matched[0].rm_so, matched[0].rm_eo); strncpy(msgbuf, &(url[matched[0].rm_so]), len); msgbuf[len] = 0; printf("|%s|\n", msgbuf); } 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); }