stage, the first three characters in the regular expression pattern have been matched. Finally, an attempt
is made to match the pattern
, which means “Match zero or more numeric characters.” Because
the character after
is a newline character, there are no numeric digits. Because there are exactly zero
numeric digits after the uppercase
, there is a match (of zero numeric digits). Because all compo-
nents of the pattern match, the whole pattern matches.
Why does the part number
also match? When the regular expression engine is at the position
immediately before the
, it attempts to match the next character in the part number with an
. Because the first character of the part number
is an uppercase
, there is a match.
Next, attempts are made to match an uppercase
and an uppercase
. These 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
, which means “Match zero or more numeric characters.” Four numeric digits
follow the uppercase
. Because there are exactly four numeric digits after the uppercase
is a match (of four numeric digits, which meets the criterion “zero or more numeric digits”). Because all
components of the pattern match, the whole pattern matches.
Work through the other part numbers step by step, and you’ll find that each ought to match the pattern
The + Quantifier
There are many situations where you will want to be certain that a character or group of characters
is present at least once but also allow for the possibility that the character occurs more than once. The
cardinality operator is designed for that situation. The
operator means “Match one or more occur-
rences of the chunk that precedes me.”
Take a look at the example with
, but look for matches that include at least one numeric digit.
You want to find part numbers that begin with the uppercase characters
and then have one or more
You can express the problem definition like this:
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 one or
more numeric digits.
Use the following pattern to express that problem definition:
Matching One or More Numeric Digits
Open OpenOffice.org Writer, and open the sample file
Use Ctrl+F to open the Find And Replace dialog box.
Check the Regular Expressions and Match Case check boxes.
Enter the pattern
in the Search For text box; click the Find All button, and inspect
the matching part numbers that are highlighted, as shown in Figure A-18.
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 334