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

Will this expression


match the string $44.95 and capture the dollar and cents amount in \1 and \2?


No. The or operator | has low precidence. The expression means

match \$


match (?:USD)([0-9]+)\.([0-9]{2})

Review of Greedy Quantifiers

Regular Expression
Group 0
Group 1 Group 2 Group 3 Group 4

As we have seen, quantifiers are greedy. This means that:

For example, when the expression


matches the string


the greedy X+ could potentially match the entire string. But it only matches the first three characters so that the final [A-Z]+ of the expression can match the final X of the string.


The strings matched by the example expression are:

strings that start with a prefix of one or more characters X, followed by one or more characters A through Z.

Find another regular expression that matches these same strings. (And use the applet to confirm that it works.)