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

Will this expression

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

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

Answer:

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

match \$

OR

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

Review of Greedy Quantifiers

Regular Expression
String
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

(X+)([A-Z]+)

matches the string

XXXX

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.


QUESTION 17:

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.)