#include <stdio.h>
#include <string.h>
#include <regex.h>
int main() {
char str[] = "This is a string with \"double-quoted substring1\", and \"double-quoted substring2\" inside.";
regex_t regex;
regmatch_t matches[2];
// Regular expression pattern to match substrings within double quotes
char pattern[] = "\"([^\"]*)\"";
// Compile regex
if (regcomp(®ex, pattern, REG_EXTENDED) != 0) {
fprintf(stderr, "Error compiling regex\n");
return 1;
}
// Allocate memory for substring storage
char extracted[128];
// Searching for matches
const char *searchPtr = str;
while (regexec(®ex, searchPtr, 2, matches, 0) == 0) {
int len = matches[1].rm_eo - matches[1].rm_so;
strncpy(extracted, searchPtr + matches[1].rm_so, len);
extracted[len] = '\0'; // Null-terminate the substring
printf("%s\n", extracted);
searchPtr += matches[0].rm_eo; // Move search pointer forward
}
// Free regex resources
regfree(®ex);
return 0;
}
/*
run:
double-quoted substring1
double-quoted substring2
*/