variables are used to give feedback to the user about what is to be matched
and the string that he or she entered.
Of course, in practice, you typically want to match a sequence of characters rather than a single character.
Matching Sequences of Characters That Each Occur Once
When the regular expression pattern
was matched, you made use of the default behavior of the regu-
lar expression engine, meaning that when there is no indication of how often a character (or sequence
of characters) is allowed to occur, the regular expression engine assumes that the character(s) in the
pattern occur exactly once, except when you include a quantifier in the regular expression pattern that
specifies an occurrence other than exactly once. This behavior also allows the matching of sequences
of the same character.
To match two characters that are the same character and occur twice without any intervening characters
(including whitespace), you can simply use a pattern with the desired character written twice in the pattern.
Matching Doubled Characters
As an example, look at how you can match sequences of characters where a character occurs exactly twice —
for example, the doubled
that can occur in words such as
A problem definition for the desired match can be expressed as follows:
Match any occurrence of the lowercase character
immediately followed by another lowercase
An example file,
, is shown here:
The arrow flew through the air at great speed.
This is a narrative of great interest to many readers.
Apples and oranges are both types of fruit.
Asses and donkeys are both four-legged mammals.
Several million barrels of oil are produced daily.
The following pattern will match all occurrences of
in the sample file:
Open OpenOffice.org Writer, and open the sample file
Use the keyboard shortcut Ctrl+F to open the Find And Replace dialog box.
Check the Regular Expressions check box and the Match Case check box.
Enter the pattern
in the Search For text box, and click the Find All button.
Figure A-5 shows
opened in OpenOffice.org Writer, as previously described. Notice that all
are matched, but single occurrences of
are not matched.
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 317