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

Rule 5

A quantified atom matches only if the atom itself matches some number of times allowed by the following quantifier. Multiple matches must be adjacent within the string. If no match can be found at a current position for any allowed quantity of the atom, the Engine backtracks to Rule 3 and tries higher-pecking-order items with different choices.

When using a flexible quantifier where the number of matches is not exact, you cannot instruct the Engine to start with the minimal number of matches and work up. The Engine always starts with the maximal number of matches and works down. The quantifiers try the biggest quantity first ("greedy matching"). The Engine first counts up to find out how many atoms it's possible to match in a row in the current string. When a choice fails, the Engine backtracks to the previous choice (the shorter one). Take a look at the following regexp:

/.*abc/

In this case the Engine tries to match the maximum number of any character (. represents "any character") until the end of the line, before it tries looking for "abc". When "abc" doesn't match there (and it can't, because there's not enough room for it at the end of the string), the Engine backs off one character at a time until it finds "abc". Once it finds "abc", it stops looking for a possible shorter match. Here's the complete table of quantifiers:

{m,n} Must occur at least m times, but not more than n times.
{n,} Must occur at least n times.
{n} Must occur exactly n times.
* Must occur 0 or more times (same as {0,}).
+ Must occur 1 or more times (same as {1,}).
? Must occur 0 or 1 time (same as {0,1}).

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