Re: [pcre-dev] PCRE Question

Top Page
Delete this message
Author: Sheri
Date:  
To: pcre-dev
Subject: Re: [pcre-dev] PCRE Question
Ashley Thomas wrote:
> Greetings.
>
> I have a question regarding PCRE.
>
> If multiple patterns (say 100 patterns) are compiled using pcre_compile()
> and then later on, a pcre search is done on an input string which
> results in a match,
> is it possible to know which pattern (among the 100) was matched?
>
> Thanks for any information,
>
>
> Ashley Thomas
>
>
>
>

pcre_exec matches only one compiled pattern against the subject string.
So that single pattern either matches or not ... the calling program can
tell you when one matches.

Possibly what you have is a single pattern constructed with multiple
alternatives.

If so, if you construct each of the alternatives as a capturing
subpattern (i.e., with parentheses around it), you should be able to
check the result for each substring of interest. pcre returns -1 for
unset captures, and the zero based string offset for subpattern matches.
Among alternatives, only one such capture in a pattern that matches
overall would be set. Also matching is done from left to right, so only
the first subpattern that matches would be set.

However, I've seen some applications that misuse PCRE's offset vector
and don't allow the retrieval of a set substring with a higher index
than other unset substrings. Also there are limits on numbers of
captures, alternatives and size of compiled patterns; they depend in
part on the calling application and the version of pcre it is using.

Regards,
Sheri