| 2318 |
documentation. |
documentation. |
| 2319 |
. |
. |
| 2320 |
. |
. |
| 2321 |
|
.\" HTML <a name="backtrackcontrol"></a> |
| 2322 |
.SH "BACKTRACKING CONTROL" |
.SH "BACKTRACKING CONTROL" |
| 2323 |
.rs |
.rs |
| 2324 |
.sp |
.sp |
| 2340 |
processed as anchored at the point where they are tested. |
processed as anchored at the point where they are tested. |
| 2341 |
.P |
.P |
| 2342 |
The new verbs make use of what was previously invalid syntax: an opening |
The new verbs make use of what was previously invalid syntax: an opening |
| 2343 |
parenthesis followed by an asterisk. In Perl, they are generally of the form |
parenthesis followed by an asterisk. They are generally of the form |
| 2344 |
(*VERB:ARG) but PCRE does not support the use of arguments, so its general |
(*VERB) or (*VERB:NAME). Some may take either form, with differing behaviour, |
| 2345 |
form is just (*VERB). Any number of these verbs may occur in a pattern. There |
depending on whether or not an argument is present. An name is a sequence of |
| 2346 |
are two kinds: |
letters, digits, and underscores. If the name is empty, that is, if the closing |
| 2347 |
|
parenthesis immediately follows the colon, the effect is as if the colon were |
| 2348 |
|
not there. Any number of these verbs may occur in a pattern. |
| 2349 |
|
.P |
| 2350 |
|
PCRE contains some optimizations that are used to speed up matching by running |
| 2351 |
|
some checks at the start of each match attempt. For example, it may know the |
| 2352 |
|
minimum length of matching subject, or that a particular character must be |
| 2353 |
|
present. When one of these optimizations suppresses the running of a match, any |
| 2354 |
|
included backtracking verbs will not, of course, be processed. You can suppress |
| 2355 |
|
the start-of-match optimizations by setting the PCRE_NO_START_OPTIMIZE option |
| 2356 |
|
when calling \fBpcre_exec()\fP. |
| 2357 |
|
. |
| 2358 |
. |
. |
| 2359 |
.SS "Verbs that act immediately" |
.SS "Verbs that act immediately" |
| 2360 |
.rs |
.rs |
| 2361 |
.sp |
.sp |
| 2362 |
The following verbs act as soon as they are encountered: |
The following verbs act as soon as they are encountered. They may not be |
| 2363 |
|
followed by a name. |
| 2364 |
.sp |
.sp |
| 2365 |
(*ACCEPT) |
(*ACCEPT) |
| 2366 |
.sp |
.sp |
| 2387 |
A match with the string "aaaa" always fails, but the callout is taken before |
A match with the string "aaaa" always fails, but the callout is taken before |
| 2388 |
each backtrack happens (in this example, 10 times). |
each backtrack happens (in this example, 10 times). |
| 2389 |
. |
. |
| 2390 |
|
. |
| 2391 |
|
.SS "Recording which path was taken" |
| 2392 |
|
.rs |
| 2393 |
|
.sp |
| 2394 |
|
There is one verb whose main purpose is to track how a match was arrived at, |
| 2395 |
|
though it also has a secondary use in conjunction with advancing the match |
| 2396 |
|
starting point (see (*SKIP) below). |
| 2397 |
|
.sp |
| 2398 |
|
(*MARK:NAME) or (*:NAME) |
| 2399 |
|
.sp |
| 2400 |
|
A name is always required with this verb. There may be as many instances of |
| 2401 |
|
(*MARK) as you like in a pattern, and their names do not have to be unique. |
| 2402 |
|
.P |
| 2403 |
|
When a match succeeds, the name of the last-encountered (*MARK) is passed back |
| 2404 |
|
to the caller via the \fIpcre_extra\fP data structure, as described in the |
| 2405 |
|
.\" HTML <a href="pcreapi.html#extradata"> |
| 2406 |
|
.\" </a> |
| 2407 |
|
section on \fIpcre_extra\fP |
| 2408 |
|
.\" |
| 2409 |
|
in the |
| 2410 |
|
.\" HREF |
| 2411 |
|
\fBpcreapi\fP |
| 2412 |
|
.\" |
| 2413 |
|
documentation. No data is returned for a partial match. Here is an example of |
| 2414 |
|
\fBpcretest\fP output, where the /K modifier requests the retrieval and |
| 2415 |
|
outputting of (*MARK) data: |
| 2416 |
|
.sp |
| 2417 |
|
/X(*MARK:A)Y|X(*MARK:B)Z/K |
| 2418 |
|
XY |
| 2419 |
|
0: XY |
| 2420 |
|
MK: A |
| 2421 |
|
XZ |
| 2422 |
|
0: XZ |
| 2423 |
|
MK: B |
| 2424 |
|
.sp |
| 2425 |
|
The (*MARK) name is tagged with "MK:" in this output, and in this example it |
| 2426 |
|
indicates which of the two alternatives matched. This is a more efficient way |
| 2427 |
|
of obtaining this information than putting each alternative in its own |
| 2428 |
|
capturing parentheses. |
| 2429 |
|
.P |
| 2430 |
|
A name may also be returned after a failed match if the final path through the |
| 2431 |
|
pattern involves (*MARK). However, unless (*MARK) used in conjunction with |
| 2432 |
|
(*COMMIT), this is unlikely to happen for an unanchored pattern because, as the |
| 2433 |
|
starting point for matching is advanced, the final check is often with an empty |
| 2434 |
|
string, causing a failure before (*MARK) is reached. For example: |
| 2435 |
|
.sp |
| 2436 |
|
/X(*MARK:A)Y|X(*MARK:B)Z/K |
| 2437 |
|
XP |
| 2438 |
|
No match |
| 2439 |
|
.sp |
| 2440 |
|
There are three potential starting points for this match (starting with X, |
| 2441 |
|
starting with P, and with an empty string). If the pattern is anchored, the |
| 2442 |
|
result is different: |
| 2443 |
|
.sp |
| 2444 |
|
/^X(*MARK:A)Y|^X(*MARK:B)Z/K |
| 2445 |
|
XP |
| 2446 |
|
No match, mark = B |
| 2447 |
|
.sp |
| 2448 |
|
PCRE's start-of-match optimizations can also interfere with this. For example, |
| 2449 |
|
if, as a result of a call to \fBpcre_study()\fP, it knows the minimum |
| 2450 |
|
subject length for a match, a shorter subject will not be scanned at all. |
| 2451 |
|
.P |
| 2452 |
|
Note that similar anomalies (though different in detail) exist in Perl, no |
| 2453 |
|
doubt for the same reasons. The use of (*MARK) data after a failed match of an |
| 2454 |
|
unanchored pattern is not recommended, unless (*COMMIT) is involved. |
| 2455 |
|
. |
| 2456 |
|
. |
| 2457 |
.SS "Verbs that act after backtracking" |
.SS "Verbs that act after backtracking" |
| 2458 |
.rs |
.rs |
| 2459 |
.sp |
.sp |
| 2460 |
The following verbs do nothing when they are encountered. Matching continues |
The following verbs do nothing when they are encountered. Matching continues |
| 2461 |
with what follows, but if there is no subsequent match, a failure is forced. |
with what follows, but if there is no subsequent match, causing a backtrack to |
| 2462 |
The verbs differ in exactly what kind of failure occurs. |
the verb, a failure is forced. That is, backtracking cannot pass to the left of |
| 2463 |
|
the verb. However, when one of these verbs appears inside an atomic group, its |
| 2464 |
|
effect is confined to that group, because once the group has been matched, |
| 2465 |
|
there is never any backtracking into it. In this situation, backtracking can |
| 2466 |
|
"jump back" to the left of the entire atomic group. (Remember also, as stated |
| 2467 |
|
above, that this localization also applies in subroutine calls and assertions.) |
| 2468 |
|
.P |
| 2469 |
|
These verbs differ in exactly what kind of failure occurs when backtracking |
| 2470 |
|
reaches them. |
| 2471 |
.sp |
.sp |
| 2472 |
(*COMMIT) |
(*COMMIT) |
| 2473 |
.sp |
.sp |
| 2474 |
This verb causes the whole match to fail outright if the rest of the pattern |
This verb, which may not be followed by a name, causes the whole match to fail |
| 2475 |
does not match. Even if the pattern is unanchored, no further attempts to find |
outright if the rest of the pattern does not match. Even if the pattern is |
| 2476 |
a match by advancing the starting point take place. Once (*COMMIT) has been |
unanchored, no further attempts to find a match by advancing the starting point |
| 2477 |
passed, \fBpcre_exec()\fP is committed to finding a match at the current |
take place. Once (*COMMIT) has been passed, \fBpcre_exec()\fP is committed to |
| 2478 |
starting point, or not at all. For example: |
finding a match at the current starting point, or not at all. For example: |
| 2479 |
.sp |
.sp |
| 2480 |
a+(*COMMIT)b |
a+(*COMMIT)b |
| 2481 |
.sp |
.sp |
| 2482 |
This matches "xxaab" but not "aacaab". It can be thought of as a kind of |
This matches "xxaab" but not "aacaab". It can be thought of as a kind of |
| 2483 |
dynamic anchor, or "I've started, so I must finish." |
dynamic anchor, or "I've started, so I must finish." The name of the most |
| 2484 |
.sp |
recently passed (*MARK) in the path is passed back when (*COMMIT) forces a |
| 2485 |
(*PRUNE) |
match failure. |
| 2486 |
.sp |
.P |
| 2487 |
This verb causes the match to fail at the current position if the rest of the |
Note that (*COMMIT) at the start of a pattern is not the same as an anchor, |
| 2488 |
pattern does not match. If the pattern is unanchored, the normal "bumpalong" |
unless PCRE's start-of-match optimizations are turned off, as shown in this |
| 2489 |
advance to the next starting character then happens. Backtracking can occur as |
\fBpcretest\fP example: |
| 2490 |
usual to the left of (*PRUNE), or when matching to the right of (*PRUNE), but |
.sp |
| 2491 |
if there is no match to the right, backtracking cannot cross (*PRUNE). |
/(*COMMIT)abc/ |
| 2492 |
In simple cases, the use of (*PRUNE) is just an alternative to an atomic |
xyzabc |
| 2493 |
group or possessive quantifier, but there are some uses of (*PRUNE) that cannot |
0: abc |
| 2494 |
be expressed in any other way. |
xyzabc\eY |
| 2495 |
|
No match |
| 2496 |
|
.sp |
| 2497 |
|
PCRE knows that any match must start with "a", so the optimization skips along |
| 2498 |
|
the subject to "a" before running the first match attempt, which succeeds. When |
| 2499 |
|
the optimization is disabled by the \eY escape in the second subject, the match |
| 2500 |
|
starts at "x" and so the (*COMMIT) causes it to fail without trying any other |
| 2501 |
|
starting points. |
| 2502 |
|
.sp |
| 2503 |
|
(*PRUNE) or (*PRUNE:NAME) |
| 2504 |
|
.sp |
| 2505 |
|
This verb causes the match to fail at the current starting position in the |
| 2506 |
|
subject if the rest of the pattern does not match. If the pattern is |
| 2507 |
|
unanchored, the normal "bumpalong" advance to the next starting character then |
| 2508 |
|
happens. Backtracking can occur as usual to the left of (*PRUNE), before it is |
| 2509 |
|
reached, or when matching to the right of (*PRUNE), but if there is no match to |
| 2510 |
|
the right, backtracking cannot cross (*PRUNE). In simple cases, the use of |
| 2511 |
|
(*PRUNE) is just an alternative to an atomic group or possessive quantifier, |
| 2512 |
|
but there are some uses of (*PRUNE) that cannot be expressed in any other way. |
| 2513 |
|
The behaviour of (*PRUNE:NAME) is the same as (*MARK:NAME)(*PRUNE) when the |
| 2514 |
|
match fails completely; the name is passed back if this is the final attempt. |
| 2515 |
|
(*PRUNE:NAME) does not pass back a name if the match succeeds. In an anchored |
| 2516 |
|
pattern (*PRUNE) has the same effect as (*COMMIT). |
| 2517 |
.sp |
.sp |
| 2518 |
(*SKIP) |
(*SKIP) |
| 2519 |
.sp |
.sp |
| 2520 |
This verb is like (*PRUNE), except that if the pattern is unanchored, the |
This verb, when given without a name, is like (*PRUNE), except that if the |
| 2521 |
"bumpalong" advance is not to the next character, but to the position in the |
pattern is unanchored, the "bumpalong" advance is not to the next character, |
| 2522 |
subject where (*SKIP) was encountered. (*SKIP) signifies that whatever text |
but to the position in the subject where (*SKIP) was encountered. (*SKIP) |
| 2523 |
was matched leading up to it cannot be part of a successful match. Consider: |
signifies that whatever text was matched leading up to it cannot be part of a |
| 2524 |
|
successful match. Consider: |
| 2525 |
.sp |
.sp |
| 2526 |
a+(*SKIP)b |
a+(*SKIP)b |
| 2527 |
.sp |
.sp |
| 2532 |
first match attempt, the second attempt would start at the second character |
first match attempt, the second attempt would start at the second character |
| 2533 |
instead of skipping on to "c". |
instead of skipping on to "c". |
| 2534 |
.sp |
.sp |
| 2535 |
(*THEN) |
(*SKIP:NAME) |
| 2536 |
|
.sp |
| 2537 |
|
When (*SKIP) has an associated name, its behaviour is modified. If the |
| 2538 |
|
following pattern fails to match, the previous path through the pattern is |
| 2539 |
|
searched for the most recent (*MARK) that has the same name. If one is found, |
| 2540 |
|
the "bumpalong" advance is to the subject position that corresponds to that |
| 2541 |
|
(*MARK) instead of to where (*SKIP) was encountered. If no (*MARK) with a |
| 2542 |
|
matching name is found, normal "bumpalong" of one character happens (the |
| 2543 |
|
(*SKIP) is ignored). |
| 2544 |
|
.sp |
| 2545 |
|
(*THEN) or (*THEN:NAME) |
| 2546 |
.sp |
.sp |
| 2547 |
This verb causes a skip to the next alternation if the rest of the pattern does |
This verb causes a skip to the next alternation if the rest of the pattern does |
| 2548 |
not match. That is, it cancels pending backtracking, but only within the |
not match. That is, it cancels pending backtracking, but only within the |
| 2553 |
.sp |
.sp |
| 2554 |
If the COND1 pattern matches, FOO is tried (and possibly further items after |
If the COND1 pattern matches, FOO is tried (and possibly further items after |
| 2555 |
the end of the group if FOO succeeds); on failure the matcher skips to the |
the end of the group if FOO succeeds); on failure the matcher skips to the |
| 2556 |
second alternative and tries COND2, without backtracking into COND1. If (*THEN) |
second alternative and tries COND2, without backtracking into COND1. The |
| 2557 |
is used outside of any alternation, it acts exactly like (*PRUNE). |
behaviour of (*THEN:NAME) is exactly the same as (*MARK:NAME)(*THEN) if the |
| 2558 |
|
overall match fails. If (*THEN) is not directly inside an alternation, it acts |
| 2559 |
|
like (*PRUNE). |
| 2560 |
. |
. |
| 2561 |
. |
. |
| 2562 |
.SH "SEE ALSO" |
.SH "SEE ALSO" |
| 2580 |
.rs |
.rs |
| 2581 |
.sp |
.sp |
| 2582 |
.nf |
.nf |
| 2583 |
Last updated: 06 March 2010 |
Last updated: 27 March 2010 |
| 2584 |
Copyright (c) 1997-2010 University of Cambridge. |
Copyright (c) 1997-2010 University of Cambridge. |
| 2585 |
.fi |
.fi |