Perl-совместимые регулярные выражения (Perl-compatible regular expressions)
Name |
Partial matching in pcre |
Частичное соответствие c PCRE (Partial matching in pcre)
In normal use of PCRE, if the subject string that is passed to a
matching function matches as far as it goes, but is too short to
match the entire pattern, PCRE_ERROR_NOMATCH is returned. There
are circumstances where it might be helpful to distinguish this
case from other cases in which there is no match.
Consider, for example, an application where a human is required
to type in data for a field with specific formatting
requirements. An example might be a date in the form ddmmmyy,
defined by this pattern:
^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$
If the application sees the user's keystrokes one by one, and can
check that what has been typed so far is potentially valid, it is
able to raise an error as soon as a mistake is made, by beeping
and not reflecting the character that has been typed, for
example. This immediate feedback is likely to be a better user
interface than a check that is delayed until the entire string
has been entered. Partial matching can also be useful when the
subject string is very long and is not all available at once.
PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT
and PCRE_PARTIAL_HARD options, which can be set when calling any
of the matching functions. For backwards compatibility,
PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. The essential
difference between the two options is whether or not a partial
match is preferred to an alternative complete match, though the
details differ between the two types of matching function. If
both options are set, PCRE_PARTIAL_HARD takes precedence.
If you want to use partial matching with just-in-time optimized
code, you must call pcre_study()
, pcre16_study()
or
pcre32_study()
with one or both of these options:
PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
PCRE_STUDY_JIT_COMPILE should also be set if you are going to run
non-partial matches on the same pattern. If the appropriate JIT
study mode has not been set for a match, the interpretive
matching code is used.
Setting a partial matching option disables two of PCRE's standard
optimizations. PCRE remembers the last literal data unit in a
pattern, and abandons matching immediately if it is not present
in the subject string. This optimization cannot be used for a
subject string that might match only partially. If the pattern
was studied, PCRE knows the minimum length of a matching string,
and does not bother to run the matching function on shorter
strings. This optimization is also disabled for partial matching.