I don't think I understand how to return only the matched regular expression. I have a file that is a webpage. I'm trying to get all the links in the page. The regex works fine. But if I printf it out it will print out the line in which that match occurs. I only want to display the match only. I see you can do grouping so I tried that and am getting back an int value for my second printf call. According to the doc it is an offset. But offset to what? It doesn't seem to be accurate either because it would say 32 when character 32 on that line has nothing to do with the regex. I put in an exit just see the first match. Where am I going wrong?
char line[1000];
FILE *fp_original;
fp_original = fopen (file_original_page, "r");
regex_t re_links;
regmatch_t group[2];
regcomp (&re_links, "(href|src)=[\"|'][^\"']*[\"|']", REG_EXTENDED);
while (fgets (line, sizeof line, fp_original) != NULL) {
if (regexec (&re_links, line, 2, group, 0) == 0) {
printf ("%s", line);
printf ("%u\n", line[group[1].rm_so]);
exit (1);
}
}
fclose (fp_original);