Other Cardinality Operators
Testing for matches only for optional characters can be very useful, as you saw in the
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
operator and the
operator, which are described in the following sections.
The * Quantifier
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,
, 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
followed by zero or more
You can express what you want to do as follows:
Match an uppercase
. If there is a match, attempt to match an uppercase
. If there is a match,
attempt to match an uppercase
. If all three uppercase characters match, attempt to match zero or
more numeric digits.
Because all the part numbers begin with the literal characters
, you can use the pattern
to match part numbers that correspond to the description in the problem definition.
Open OpenOffice.org Writer and open the sample file,
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 332