Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 6.18 Why don't word-boundary searches with "\b" work for me?

Reply
Thread Tools

FAQ 6.18 Why don't word-boundary searches with "\b" work for me?

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      04-24-2011
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

6.18: Why don't word-boundary searches with "\b" work for me?

(contributed by brian d foy)

Ensure that you know what \b really does: it's the boundary between a
word character, \w, and something that isn't a word character. That
thing that isn't a word character might be \W, but it can also be the
start or end of the string.

It's not (not!) the boundary between whitespace and non-whitespace, and
it's not the stuff between words we use to create sentences.

In regex speak, a word boundary (\b) is a "zero width assertion",
meaning that it doesn't represent a character in the string, but a
condition at a certain position.

For the regular expression, /\bPerl\b/, there has to be a word boundary
before the "P" and after the "l". As long as something other than a word
character precedes the "P" and succeeds the "l", the pattern will match.
These strings match /\bPerl\b/.

"Perl" # no word char before P or after l
"Perl " # same as previous (space is not a word char)
"'Perl'" # the ' char is not a word char
"Perl's" # no word char before P, non-word char after "l"

These strings do not match /\bPerl\b/.

"Perl_" # _ is a word char!
"Perler" # no word char before P, but one after l

You don't have to use \b to match words though. You can look for
non-word characters surrounded by word characters. These strings match
the pattern /\b'\b/.

"don't" # the ' char is surrounded by "n" and "t"
"qep'a'" # the ' char is surrounded by "p" and "a"

These strings do not match /\b'\b/.

"foo'" # there is no word char after non-word '

You can also use the complement of \b, \B, to specify that there should
not be a word boundary.

In the pattern /\Bam\B/, there must be a word character before the "a"
and after the "m". These patterns match /\Bam\B/:

"llama" # "am" surrounded by word chars
"Samuel" # same

These strings do not match /\Bam\B/

"Sam" # no word boundary before "a", but one after "m"
"I am Sam" # "am" surrounded by non-word chars



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.36 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? PerlFAQ Server Perl Misc 0 04-26-2011 10:00 AM
FAQ 6.18 Why don't word-boundary searches with "\b" work for me? PerlFAQ Server Perl Misc 0 01-31-2011 11:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments