Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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
You can’t perform that action at this time.