JavaScript Editor Ajax software     Free javascripts 



Main Page

attempts are made to match an uppercase
B
and an uppercase
C
. They too match. At that stage, the first
three characters in the regular expression pattern have been matched. Finally, an attempt is made to
match the pattern
[0-9]+
, which means “Match one or more numeric characters.” Four numeric digits
follow the uppercase
C
of
ABC
, so there is a match (of four numeric digits, which meets the criterion “one
or more numeric digits”). Because all components of the pattern match, the whole pattern matches.
Before moving on to look at the curly-brace quantifier syntax, here’s a brief review of the quantifiers
already discussed, as listed in Table A-1.
Table A-1
These quantifiers can often be useful, but there are times when you will want to express ideas such as
“Match something that occurs at least twice but can occur an unlimited number of times” or “Match
something that can occur at least three times but no more than six times.”
You also saw earlier that you can express a repeating character by simply repeating the character in a
regular expression pattern.
The Curly-Brace Syntax
If you want to specify large numbers of occurrences, you can use a curly-brace syntax to specify an exact
number of occurrences.
The {n} Syntax
Suppose that you want to match part numbers with sequences of characters that have exactly three
numeric digits. You can write the pattern as
ABC[0-9][0-9][0-9]
by simply repeating the character class for a numeric digit. Alternatively, you can use the curly-brace
syntax and write
ABC[0-9]{3}
to achieve the same result.
Most regular expression engines support a syntax that can express ideas like that. The syntax uses curly
braces to specify minimum and maximum numbers of occurrences.
Quantifier
Definition
?
0 or 1 occurrences
*
0 or more occurrences
+
1 or more occurrences
336
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 336


JavaScript Editor Ajax software     Free javascripts