Added gnu_regex2.c

This commit is contained in:
anon 2024-03-10 15:57:35 +01:00
parent 255a93c5e5
commit 2372ccb95f

23
gnu_regex2.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <regex.h>
int main() {
regex_t regex;
regcomp(&regex, "(pattern) (example)", REG_EXTENDED);
char test_str[] = "This is a pattern example.";
regmatch_t pmatch[3]; // One for the whole match, and one for each group
if (regexec(&regex, test_str, 3, pmatch, 0) == 0) {
printf("Whole match: %.*s\n", pmatch[0].rm_eo - pmatch[0].rm_so, test_str + pmatch[0].rm_so);
printf("Group 1: %.*s\n", pmatch[1].rm_eo - pmatch[1].rm_so, test_str + pmatch[1].rm_so);
printf("Group 2: %.*s\n", pmatch[2].rm_eo - pmatch[2].rm_so, test_str + pmatch[2].rm_so);
} else {
printf("Pattern not found.\n");
}
regfree(&regex);
return 0;
}