Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pcre C API only return first match

Tags:

c

regex

pcre

#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define OVECCOUNT 30
#define SRCBUFFER 1024*1024

int main(int argc, char **argv){
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
if (argc != 2){
    fprintf(stderr, "Usage : %s PATTERN\n", argv[0]);
    return 1;
}

char *src=malloc(SRCBUFFER);
int srclen = fread(src, sizeof(char), SRCBUFFER, stdin);
re = pcre_compile(argv[1], 0, &error, &erroffset, NULL);
if (re == NULL){
    fprintf(stderr, "PCRE compilation failed at offset %d: %s\n", erroffset, error);
    return 1;
}

rc = pcre_exec(re, NULL, src, srclen, 0, 0, ovector, OVECCOUNT);
if (rc < 0){
    if (rc == PCRE_ERROR_NOMATCH) fprintf(stderr, "Sorry, no match...\n");
    else fprintf(stderr, "Matching error %d\n", rc);
    return 1;
}

for (i = 0; i < rc; i++){
    char *substring_start = src + ovector[2 * i];
    int substring_length = ovector[2 * i + 1] - ovector[2 * i];
    fprintf(stdout, "%2d: %.*s\n", i, substring_length, substring_start);
}
return 0;
}

run it

echo "apple banana africa" | ./program '\ba\w+\b'

and it print

0: apple

I've tried to use the PCRE_MULTILINE option,but no use.How to make it print all matchs?

like image 757
riaqn Avatar asked Dec 08 '25 09:12

riaqn


1 Answers

It sounds like what you're looking for is the equivalent of the Perl /g regex flag to repeat the match as many times as possible and return the results of all the matches. I don't believe PCRE has anything like that.

Instead, you will need to add a loop around pcre_exec. Each time you call it, it will return the byte offset of the start and end of the match. You want to then run pcre_exec again on the string starting at the end of the match. Repeat until pcre_exec doesn't match.

like image 165
rra Avatar answered Dec 09 '25 23:12

rra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!