Ajax software
Free javascripts
↑
Main Page
Figure A-14
When the regular expression engine reaches a position just before the
c
of
color
, it attempts to match a lower-
case
c
. This match succeeds. It next attempts to match a lowercase
o
. That too matches. It next attempts to
match a lowercase
l
and a lowercase
o
. They match as well. It then attempts to match the pattern
u?
, which
means zero or one lowercase
u
characters. Because there are exactly zero lowercase
u
characters following the
lowercase
o
, there is a match. The pattern
u?
matches zero characters. Finally, it attempts to match the final
character in the pattern — that is, the lowercase
r
. Because the next character in the string
color
does match
a lowercase
r
, the whole pattern is matched.
There is no match in the line
His collar is too tight or too colouuuurful
. The only possible match
might be in the sequence of characters
colouuuurful
. The failure to match occurs when the regular expres-
sion engine attempts to match the pattern
u?
. Because the pattern
u?
means “match zero or one lowercase u
characters,” there is a match on the first u of
colouuuurful
. After that successful match, the regular expres-
sion engine attempts to match the final character of the pattern
colou?r
against the second lowercase
u
in
colouuuurful
. That attempt to match fails, so the attempt to match the whole pattern
colou?r
against the
sequence of characters
colouuuurful
also fails.
What happens when the regular expression engine attempts to find a match in the line
These are bright
colours.
?
When the regular expression engine reaches a position just before the
c
of
colours
, it attempts to match
a lowercase
c
. That match succeeds. It next attempts to match a lowercase
o
, a lowercase
l
, and another
lowercase
o
. These also match. It next attempts to match the pattern
u?
, which means zero or one lower-
case
u
characters. Because exactly one lowercase
u
character follows the lowercase
o
in
colours
, there is
a match. Finally, the regular expression engine attempts to match the final character in the pattern, the
lowercase
r
. Because the next character in the string
colours
does match a lowercase
r
, the whole pattern
is matched.
The
findstr
utility can also be used to test for the occurrence of the sequence of characters
color
and
colour
, but the regular expression engine in the
findstr
utility has a limitation in that it lacks a metachar-
acter to signify an optional character. For many purposes, the
*
metacharacter, which matches zero, one, or
more occurrences of the preceding character, will work successfully.
327
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 327
Ajax software
Free javascripts
→