[A-Za-z]+[A-Za-z0-9]*
Regular Expression (Delimit with quotes) |
|
String (Delimit with quotes) |
|
Rule 8. Greedy Matching
Here is a regular expression:
X+X+
Does this regular expression match the following string?
XX
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:
+
quantifier matches as many characters as it can without preventing the
rest of the regular expression from matching.
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?