[~/src/C/regexp]$ cat use_posix.c
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
int main() {
regex_t regex;
int reti;
char msgbuf[100];
/* 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);
}
Compilación y ejecución:
[~/src/C/regexp]$ cc use_posix.c -o use_posix [~/src/C/regexp]$ ./use_posix Match
Casiano Rodríguez León