Maybe regular expressions are not the right tool here. You can find your subexpressions simply by searching for your fixed delimiting strings:
const char *p = str;
for (;;) {
const char *q, *r;
int len;
r = strstr(p, "<$ABC>");
if (r == NULL) break;
r += 6;
q = strstr(r + 6, "<ABC$>");
if (q == NULL) break;
len = q - r;
printf("'%.*s'\n", len, r);
p = q + 6;
}
Edit: As pointed out in the comments, the captured string between delimiters must of course start after the beginning delimiter, but strstr
finds its beginning. Hence, the length of the delimiting string, here 6, must be added to r
.
In the same way, if the captured string should include the delimiters, r
stays as it is after the strstr
search and the length of the end delimiter must be added to q
:
r = strstr(p, "<$ABC>");
if (r == NULL) break;
q = strstr(r + 6, "<ABC$>");
if (q == NULL) break;
q += 6;
len = q - r;
printf("'%.*s'\n", len, r);
p = q;