Ajax software
Free javascripts
↑
Main Page
Other Cardinality Operators
Testing for matches only for optional characters can be very useful, as you saw in the
colors
example,
but it would be pretty limiting if that were the only quantifier available to a developer. Most regular
expression implementations provide two other cardinality operators (also called
quantifiers
): the
*
operator and the
+
operator, which are described in the following sections.
The * Quantifier
The
*
operator refers to zero or more occurrences of the pattern to which it is related. In other words,
a character or group of characters is optional but may occur more than once. Zero occurrences of the
chunk that precedes the
*
quantifier should match. A single occurrence of that chunk should also match.
So should two occurrences, three occurrences, and ten occurrences. In principle, an unlimited number of
occurrences will also match.
Try this out in an example using OpenOffice.org Writer.
Matching Zero or More Occurrences
The sample file,
Parts.txt
, contains a listing of part numbers that have two alphabetic characters followed
by zero or more numeric digits. In the simple sample file, the maximum number of numeric digits is three,
but because the
*
quantifier will match three occurrences, you can use it to match the sample part numbers. If
there is a good reason why it is important that a maximum of three numeric digits can occur, you can express
that notion by using an alternative syntax, which is discussed a little later in this appendix. Each of the part
numbers in this example consists of the sequence of uppercase characters
ABC
followed by zero or more
numeric digits:
ABC
ABC123
ABC12
ABC889
ABC8899
ABC34
You can express what you want to do as follows:
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 zero or
more numeric digits.
Because all the part numbers begin with the literal characters
ABC
, you can use the pattern
ABC[0-9]*
to match part numbers that correspond to the description in the problem definition.
1.
Open OpenOffice.org Writer and open the sample file,
Parts.txt
.
332
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 332
Ajax software
Free javascripts
→