diff options
author | Yves Orton <demerphq@gmail.com> | 2006-10-31 23:49:57 +0100 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2006-11-01 17:05:03 +0000 |
commit | 5461259206276a3618e115d5d68776273bb41ca6 (patch) | |
tree | 62be0b5e283fa9c04597fe6ec054cc83064e0f87 /pod | |
parent | a983b08dc7c658d412bac21b8ceac00c24ae5c11 (diff) | |
download | perl-5461259206276a3618e115d5d68776273bb41ca6.tar.gz |
Add a commit verb to regex engine to allow fine tuning of backtracking control.
Message-ID: <9b18b3110610311349n5947cc8fsf0b2e6ddd9a7ee01@mail.gmail.com>
p4raw-id: //depot/perl@29183
Diffstat (limited to 'pod')
-rw-r--r-- | pod/perlre.pod | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/pod/perlre.pod b/pod/perlre.pod index b46b3811d0..4e683a37f4 100644 --- a/pod/perlre.pod +++ b/pod/perlre.pod @@ -1046,6 +1046,54 @@ to inside of one of these constructs. The following equivalences apply: PAT?+ (?>PAT?) PAT{min,max}+ (?>PAT{min,max}) +=item C<(?COMMIT)> +X<(?COMMIT)> + +This zero-width pattern commits the match at the current point, preventing +the engine from back-tracking on failure to the left of the commit point. +Consider the pattern C<A (?COMMIT) B>, where A and B are complex patterns. +Until the C<(?COMMIT)> is reached, A may backtrack as necessary to match. +Once it is reached, matching continues in B, which may also backtrack as +necessary; however, should B not match, then no further backtracking will +take place, and the pattern will fail outright at that starting position. + +The following example counts all the possible matching strings in a +pattern (without actually matching any of them). + + 'aaab'=~/a+b?(?{print "$&\n"; $count++})(?FAIL)/; + print "Count=$count\n"; + +which produces: + + aaab + aaa + aa + a + aab + aa + a + ab + a + Count=9 + +If we add a C<(?COMMIT)> before the count like the following + + 'aaab'=~/a+b?(?COMMIT)(?{print "$&\n"; $count++})(?FAIL)/; + print "Count=$count\n"; + +we prevent backtracking and find the count of the longest matching +at each matching startpoint like so: + + aaab + aab + ab + Count=3 + +Any number of C<(?COMMIT)> assertions may be used in a pattern. + +See also C<< (?>pattern) >> and possessive quantifiers for other +ways to control backtracking. + =item C<(?(condition)yes-pattern|no-pattern)> X<(?()> |