Skip to content

Instantly share code, notes, and snippets.

@sineer
Created October 9, 2014 05:41
Show Gist options
  • Save sineer/63e9c7d02780ded70d33 to your computer and use it in GitHub Desktop.
Save sineer/63e9c7d02780ded70d33 to your computer and use it in GitHub Desktop.
#include <regex.h>
int main() {
regex_t regex;
int ret;
chat msg[128];
ret = regcomp(&regex, "^P[^:alnum:]", 0);
if (!ret) {
printf("Coulnd not compile regex\n");
exit(1);
}
// Execute the Regular Expression
ret = regexec(&regex, "PAPA", 0, NULL, 0);
if (!ret) {
printf("MATCH!");
}
else if (ret == RET_NOMATCH) {
printf("NO MATCH.");
}
else {
regerror(ret, &regex, msg, sizeof(msg));
printf("REGEX ERROR: %s\n", msg);
exit(1);
}
// Execute same Regular Expression that should NOT MATCH "P123"
ret = regexec(&regex, "P123", 0, NULL, 0);
if (!ret) {
printf("MATCH!");
}
else if (ret == RET_NOMATCH) {
printf("NO MATCH.");
}
else {
regerror(ret, &regex, msg, sizeof(msg));
printf("REGEX ERROR: %s\n", msg);
exit(1);
}
// FREE the Regex!
regfree(&regex);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment