From 2372ccb95fc3fb4f790f3ee6023d0fee90260653 Mon Sep 17 00:00:00 2001 From: anon Date: Sun, 10 Mar 2024 15:57:35 +0100 Subject: [PATCH] Added gnu_regex2.c --- gnu_regex2.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 gnu_regex2.c diff --git a/gnu_regex2.c b/gnu_regex2.c new file mode 100644 index 0000000..b5e4a06 --- /dev/null +++ b/gnu_regex2.c @@ -0,0 +1,23 @@ +#include +#include + +int main() { + regex_t regex; + + regcomp(®ex, "(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(®ex, 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(®ex); + + return 0; +}