I am using regular expressions in C (using the "regex.h" library). After setting up the standard calls (and checks) for regcomp(...) and regexec(...), I can only manage to print the actual substrings that match my compiled regular expression. Using regexec, according to the manual pages, means you store the substring matches in a structure known as "regmatch_t". The struct only contains rm_so and rm_eo to reference what I understand to be the addresses of the characters of the matched substring in memory, but my question is how can I just use these to offsets and two pointers to extract the actual substring and store it into an array (ideally a 2D array of strings)?
It works when you just print to standard out, but whenever you try to use the same setup but store it in a string/character array, it stores the entire string that was originally used to match against the expression. Further, what is the "%.*s" inside the print statement? I imagine it's a regular expression in of itself to read in the pointers to a character array correctly. I just want to store the matched substrings inside a collection so I can work with them elsewhere in my software.
Background: p and p2 are both pointers set to point to the start of string to match before entering the while loop in the code below: [EDIT: "matches" is a 2D array meant to ultimately store the substring matches and was preallocated/initalized before the main loop you see below]
int ind = 0;
while(1){
regExErr1 = regexec(&r, p, 10, m, 0);
//printf("Did match regular expr, value %i
", regExErr1);
if( regExErr1 != 0 ){
fprintf(stderr, "No more matches with the inherent regular expression!
");
break;
}
printf("What was found was: ");
int i = 0;
while(1){
if(m[i].rm_so == -1){
break;
}
int start = m[i].rm_so + (p - p2);
int finish = m[i].rm_eo + (p - p2);
strcpy(matches[ind], ("%.*s
", (finish - start), p2 + start));
printf("Storing: %.*s", matches[ind]);
ind++;
printf("%.*s
", (finish - start), p2 + start);
i++;
}
p += m[0].rm_eo; // this will move the pointer p to the end of last matched pattern and on to the start of a new one
}
printf("We have in [0]: %s
", temp);
See Question&Answers more detail:os