go to previous page   go to home page   go to next page



Basic Regular Expressions: Greedy

Regular Expression
(Delimit with quotes)
(Delimit with quotes)

Rule 8. Greedy Matching

Here is a regular expression:


Does this regular expression match the following string?


It does. But there might be a problem. The first part of the regular expression X+ could potentially match the entire string, leaving the last X+ with nothing to match (but it must match at least one character to succeed).

Here is what is going on:

This style of matching is called greedy. When X+X+ matches XX the first part of the RE matches only the first X because matching the second X would prevent the rest of the RE from matching.

The RE XX+ corresponds to the same set of strings as the previous RE, and is much easier to understand. Usually you should write expressions that avoid the need for greedy behavior.

(There are other styles of matching besides "greedy". These other styles are discussed later in these chapters.)


Here is the RE again:   X+X+

And here is a string: XXXX

What part of the string does the first   X+   match?