Use POSIX regex in C

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(&regex, “^a[[:alnum:]]”, 0);
if( reti ){ fprintf(stderr, “Could not compile regex\n”); exit(1); }

/* Execute regular expression */
reti = regexec(&regex, “abc”, 0, NULL, 0);
if( !reti ){
puts(“Match”);
}
else if( reti == REG_NOMATCH ){
puts(“No match”);
}
else{
regerror(reti, &regex, 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(&regex);

/* The other examples */
reti = regcomp(&regex, “\\([0-9]+ ms\\) no”, REG_EXTENDED);
if (reti)
{
fprintf(stderr, “Could not compile regex\n”);
return -1;
}

reti = regexec(&regex, “(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(&regex, “(x ms) yes”, 0, NULL, 0);
if (reti == REG_NOMATCH)
puts(“No match here – x ms”);

reti = regexec(&regex, “(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(&regex);
reti = regcomp(&regex, “ab+c”, REG_EXTENDED);
if (reti)
{
fprintf(stderr, “Cound not compile regex\n”);
return -1;
}

reti = regexec(&regex, “abc”, 0, NULL, 0);
if (!reti)
puts(“match again – abc”);

reti = regexec(&regex, “abbbc”, 0, NULL, 0);
if (!reti)
puts(“match again – abbbc”);

reti = regexec(&regex, “ac”, 0, NULL, 0);
if (!reti)
puts(“match again – ac?”);
else if (reti == REG_NOMATCH)
puts(“no match here – ac”);

return 0;
}

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.