An Introduction to Regular Expressions Matching Rules 43 of 50 : [PREV] [NEXT]

Rule 3

Any specific alternative matches if every item in the alternatives matches sequentially according to Rules 4 and 5. An item can consist of an assertion or a quantified atom. If the item cannot be matched in order (the literal order), the Engine backtracks to the next alternative according to Rule 2 (if no alternatives are left, there is no match).

Sequential items aren't separated by any sort of punctuation. They are simply specified in the order they must match. Take a look at the following pattern:

/^Bart/

It consists of five items to be matched in order. The first one is a zero-width assertion (matches the beginning of a string), and the other four are simple characters that match themselves sequentially.

Items that have multiple choices (such as "match one or more...") are given "pecking-order" from left to right. This means that in a pattern like:

/x*y*/

x picks one way to match, and then y tries all its ways to match. If that fails, x picks another way to match, and then y tries all its ways again. This process continues until a match is found or until no options remain. Think of this as scanning a multi-dimensional array, where y reflects the counter of the inner loop, and x reflects the counter of the outer loop. Since y varies faster, one can borrow a phrase from multi-dimensional arrays: "The items to the right vary faster."


© 2003 Barbie barbie@missbarbell.co.uk Home http://birmingham.pm.org/