Reputation: 6088
In the script I'm reading about regular expressions it says:
'\b'
hits the front border of the word'\<'
hits the start of the wordSo what's the difference in using the following
\b
over \<
\b
over \>
Upvotes: 2
Views: 209
Reputation: 66263
My man grep
tells me about \b
:
The symbols
\<
and\>
respectively match the empty string at the beginning and end of a word. The symbol\b
matches the empty string at the edge of a word, [...]
So \bfoo\b
would match wherever \<foo\>
would match.
On the other hand: There are so many regexp variants, that it is hard to tell what yours is doing with \b
.
Upvotes: 4
Reputation: 385897
Your source appears to be wrong, or at least incomplete. \b
matches any border, not just the front one. Quote man grep
:
The symbols\<
and\>
respectively match the empty string at the beginning and end of a word. The symbol\b
matches the empty string at the edge of a word
grep
's \b
is equivalent to grep
's \(\<\|\>\)
In case you are familiar with Perl regular expressions,
grep
's \<
is equivalent to Perl's (?<!\w)(?=\w)
grep
's \>
is equivalent to Perl's (?<=\w)(?!\w)
grep
's \b
is equivalent to Perl's \b
grep
's \b
is equivalent to Perl's (?:(?<!\w)(?=\w)|(?<=\w)(?!\w))
Upvotes: 3
Reputation: 28005
\b
is like \<
and \>
combined:
\<
match at beginning of word,\>
match at end of word,\b
match at the begining OR end of word,\B
matches except at the beginning or end of a word.Upvotes: 4