Word Boundaries (Page 10)
The \b
metacharacter is an anchor, similar to the caret (^
) and dollar sign ($
). It matches a zero-length position called a word boundary. Word boundaries allow you to perform “whole word” searches in a string using patterns like \bword\b
.
What is a Word Boundary?
A word boundary occurs at three possible positions in a string:
- Before the first character if it is a word character.
- After the last character if it is a word character.
- Between two characters where one is a word character and the other is a non-word character.
A word character includes letters, digits, and the underscore ([a-zA-Z0-9_]
). Non-word characters are everything else.
Example Usage
The pattern \bword\b
matches the word "word" only if it appears as a standalone word in the text.
Regex | String | Matches |
---|---|---|
\b4\b
|
"There are 44 sheets" | No |
\b4\b
|
"Sheet number 4 is here" | Yes |
Digits are considered word characters, so \b4\b
will match a standalone "4" but not when it is part of "44."
Negated Word Boundaries
The \B
metacharacter is the negated version of \b
. It matches any position that is not a word boundary.
Regex | String | Matches |
---|---|---|
\Bis\B
|
"This is a test" | No |
\Bis\B
|
"This island is beautiful" | Yes |
\Bis\B
would match "is" only if it appears within a word, such as in "island," but not if it appears as a standalone word.
Looking Inside the Regex Engine
Let’s see how the regex \bis\b
works on the string "This island is beautiful":
-
The engine starts with
\b
at the first character "T." Since\b
is zero-width, it checks the position before "T." It matches because "T" is a word character, and the position before it is the start of the string. -
The engine then checks the next token,
i
, which does not match "T," so it moves to the next position. -
The engine continues checking until it finds a match at the second "is." The final
\b
matches before the space after "is," confirming a complete match.
Tcl Word Boundaries
Most regex flavors use \b
for word boundaries. However, Tcl uses different syntax:
-
\y
matches a word boundary. -
\Y
matches a non-word boundary. -
\m
matches only the start of a word. -
\M
matches only the end of a word.
For example, in Tcl:
-
\mword\M
matches "word" as a whole word.
In most flavors, you can achieve the same with \bword\b
.
Emulating Tcl Word Boundaries
If your regex flavor supports lookahead and lookbehind, you can emulate Tcl’s \m
and \M
:
-
(?<!\w)(?=\w)
: Emulates\m
. -
(?<=\w)(?!\w)
: Emulates\M
.
For flavors without lookbehind, use:
-
\b(?=\w)
to emulate\m
. -
\b(?!\w)
to emulate\M
.
GNU Word Boundaries
GNU extensions to POSIX regular expressions support \b
and \B
. Additionally, GNU regex introduces:
-
\<
: Matches the start of a word (like Tcl’s\m
). -
\>
: Matches the end of a word (like Tcl’s\M
).
These additional tokens provide flexibility when working with word boundaries in GNU-based tools.
Summary
Word boundaries are crucial for identifying standalone words in text. They prevent partial matches within larger words and ensure more precise regex patterns. Understanding how to use \b
, \B
, and their equivalents in various regex flavors will help you craft better, more accurate regular expressions.
0 Comments
Recommended Comments
There are no comments to display.