There is no standard regex lib in glibc. However, almost all UNIXs, Linux and even Mac OS should support POSIX regex, which provides a way to do regex processing in C. Below is the real example under Linux. For Mac OS, different header files may be needed.
0. The different between POSIX basic regex and extended regex
http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended
In a word, to use “*|+|?”, extended mode should be chosen in regcomp. Detailed difference please refer to the link above.
/*
* Use regex in C with POSIX regex
* Reference: http://stackoverflow.com/questions/1085083/regular-expressions-in-c-examples
* root@davejingtian.org
* http://davejingtian.org
* Aug 22, 2013
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
int main()
{
regex_t regex;
int reti;
char msgbuf[100] = {0};
/* Compile regular expression */
reti = regcomp(®ex, “^a[[:alnum:]]”, 0);
if( reti ){ fprintf(stderr, “Could not compile regex\n”); exit(1); }
/* Execute regular expression */
reti = regexec(®ex, “abc”, 0, NULL, 0);
if( !reti ){
puts(“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);
/* The other examples */
reti = regcomp(®ex, “\\([0-9]+ ms\\) no”, REG_EXTENDED);
if (reti)
{
fprintf(stderr, “Could not compile regex\n”);
return -1;
}
reti = regexec(®ex, “(4 ms) no”, 0, NULL, 0);
if (!reti)
puts(“Match again – 4 ms”);
else if (reti == REG_NOMATCH)
puts(“No match here – 4 ms?”);
reti = regexec(®ex, “(x ms) yes”, 0, NULL, 0);
if (reti == REG_NOMATCH)
puts(“No match here – x ms”);
reti = regexec(®ex, “(44 ms) no”, 0, NULL, 0);
if (!reti)
puts(“Match again – 44 ms”);
else if (reti == REG_NOMATCH)
puts(“No match here – 44 ms?”);
/* Yet another stupid example */
regfree(®ex);
reti = regcomp(®ex, “ab+c”, REG_EXTENDED);
if (reti)
{
fprintf(stderr, “Cound not compile regex\n”);
return -1;
}
reti = regexec(®ex, “abc”, 0, NULL, 0);
if (!reti)
puts(“match again – abc”);
reti = regexec(®ex, “abbbc”, 0, NULL, 0);
if (!reti)
puts(“match again – abbbc”);
reti = regexec(®ex, “ac”, 0, NULL, 0);
if (!reti)
puts(“match again – ac?”);
else if (reti == REG_NOMATCH)
puts(“no match here – ac”);
return 0;
}