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

Here is the RE again:   X+X+

And here is a string: XXXX

Answer:

Since it is greedy, the first X+ matches as much as it can without blocking the entire RE from matching, which is XXX . The final 'X' is matched by last part of the RE.

Bug Alert! It is tempting to think that each half of X+X+ matches the same number of chracters X. But this is not so.


Other Greedy Quantifiers

The star * plus + and ? quantifiers are all greedy. Each of them matches as much as it can without preventing a match between the complete RE and the string. Here is a RE

A*A*

and here is a string

AAAAAAAA

The RE matches the string. In this case, the first A* in the RE matches the entire string, since the second A* will then match zero characters and the entire match will still succeed.


QUESTION 22:

Here is a RE   Z*Z+   and here is a string it matches:  ZZZZ .

What part of the string does   Z*  match?

Is there a simpler RE that matches the same set of strings?