By default, a regular expression will try to find the first match for
the pattern in a string. Match
/iss/
against the string
``Mississippi,'' and it will find the substring ``iss'' starting at
position one. But what if you want to force a pattern to match only at
the start or end of a string?
The patterns
^
and
$
match the beginning and end of a
line, respectively. These are often used to
anchor a pattern
match: for example,
/^option/
matches the word ``option'' only
if it appears at the start of a line. The sequence
\A
matches
the beginning of a string, and
\z
and
\Z
match the
end of a string. (Actually,
\Z
matches the end of a string
unless the string ends with a ``\n'', it which case it
matches just before the ``\n''.)
showRE("this is\nthe time", /^the/)
|
� |
this is\n<<the>> time
|
showRE("this is\nthe time", /is$/)
|
� |
this <<is>>\nthe time
|
showRE("this is\nthe time", /\Athis/)
|
� |
<<this>> is\nthe time
|
showRE("this is\nthe time", /\Athe/)
|
� |
no match
|
Similarly, the patterns
\b
and
\B
match word boundaries
and nonword boundaries, respectively. Word characters are letters,
numbers, and underscore.
showRE("this is\nthe time", /\bis/)
|
� |
this <<is>>\nthe time
|
showRE("this is\nthe time", /\Bis/)
|
� |
th<<is>> is\nthe time
|