Ajax software
Free javascripts
↑
Main Page
The {n,m} Syntax
The
*
operator that was described a little earlier in this appendix effectively means “Match a minimum
of zero occurrences and a maximum occurrence, which is unbounded.” Similarly, the
+
quantifier means
“Match a minimum of one occurrence and a maximum occurrence, which is unbounded.”
Using curly braces and numbers inside them allows the developer to create occurrence quantifiers that
cannot be specified when using the
?
,
*
, or
+
quantifiers.
The following subsections look at three variants that use the curly-brace syntax. First, look at the syntax
that specifies “Match zero or up to [a specified number] of occurrences.”
{0,m}
The
{0,m}
syntax allows you to specify that a minimum of zero occurrences can be matched (specified
by the first numeric digit after the opening curly brace) and that a maximum of
m
occurrences can be
matched (specified by the second numeric digit, which is separated from the minimum occurrence indi-
cator by a comma and which precedes the closing curly brace).
To match a minimum of zero occurrences and a maximum of one occurrence, you would use the pattern
{0,1}
which has the same meaning as the
?
quantifier.
To specify matching of a minimum of zero occurrences and a maximum of three occurrences, you would
use the pattern
{0,3}
which you couldn’t express using the
?
,
*
, or
+
quantifiers.
Suppose that you want to specify that you want to match the sequence of characters
ABC
followed by a
minimum of zero numeric digits or a maximum of two numeric digits.
You can semiformally express that as the following problem definition:
Match an uppercase
A
. If there is a match, attempt to match an uppercase
B
. If there is a match, attempt
to match an uppercase
C
. If all three uppercase characters match, attempt to match a minimum of zero
or a maximum of two numeric digits.
The following pattern does what you need:
ABC[0-9]{0,2}
The
ABC
simply matches a sequence of the corresponding literal characters. The
[0-9]
indicates that a
numeric digit is to be matched, and the
{0,2}
is a quantifier that indicates a minimum of zero occur-
rences of the preceding chunk (which is
[0-9]
, representing a numeric digit) and a maximum of two
occurrences of the preceding chunk is to be matched.
337
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 337
Ajax software
Free javascripts
→