Ajax software
Free javascripts
↑
Main Page
Again, the
myRegExp
and
entry
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
L
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
r
that can occur in words such as
arrow
and
narrative
.
A problem definition for the desired match can be expressed as follows:
Match any occurrence of the lowercase character
r
immediately followed by another lowercase
r
.
An example file,
DoubledR.txt
, 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
rr
in the sample file:
rr
1.
Open OpenOffice.org Writer, and open the sample file
DoubledR.txt
.
2.
Use the keyboard shortcut Ctrl+F to open the Find And Replace dialog box.
3.
Check the Regular Expressions check box and the Match Case check box.
4.
Enter the pattern
rr
in the Search For text box, and click the Find All button.
Figure A-5 shows
DoubledR.txt
opened in OpenOffice.org Writer, as previously described. Notice that all
occurrences of
rr
are matched, but single occurrences of
r
are not matched.
317
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 317
Ajax software
Free javascripts
→