Ajax software
Free javascripts
↑
Main Page
Figure A-7
Try each of the strings from
ABC123.txt
. You can also create your own test string. Notice that the pattern
\d\d\d
will match any sequence of three successive numeric digits, but single numeric digits or pairs of
numeric digits are not matched.
The regular expression engine looks for a numeric digit. If the first character that it tests is not a numeric
digit, it moves one character through the test string and then tests whether that character matches a
numeric digit. If not, it moves one character further and tests again.
If a match is found for the first occurrence of
\d
, the regular expression engine tests if the next character is
also a numeric digit. If that matches, a third character is tested to determine if it matches the
\d
metachar-
acter for a numeric digit. If three successive characters are each a numeric digit, there is a match for the
regular expression pattern
\d\d\d
.
You can see this matching process in action by using the Komodo Regular Expressions Toolkit. Open the
Komodo Regular Expression Toolkit, and clear any existing regular expression and test string. Enter the
test string
A234BC
; then, in the area for the regular expression pattern, enter the pattern
\d
. You will see
that the first numeric digit,
2
, is highlighted as a match. Add a second
\d
to the regular expression area,
and you will see that
23
is highlighted as a match. Finally, add a third
\d
to give a final regular expres-
sion pattern
\d\d\d
, and you will see that
234
is highlighted as a match. See Figure A-8.
You can try this with other test text from
ABC123.txt
. I suggest that you also try this out with your own
test text that includes numeric digits and see which test strings match. You may find that you need to add
a space character after the test string for matching to work correctly in the Komodo Regular Expression
Toolkit.
Why was JavaScript used for the preceding example? Because you can’t use OpenOffice.org Writer to
test matches for the
\d
metacharacter.
Matching numeric digits can pose difficulties. Figure A-9 shows the result of an attempted match in
ABC123.txt
when using OpenOffice.org Writer with the pattern
\d\d\d
.
321
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 321
Ajax software
Free javascripts
→