Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Why don't word-boundary searches with <CODE>\b</CODE> work for me?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:25 UTC ( [id://672]=perlfaq nodetype: print w/replies, xml ) Need Help??

Current Perl documentation can be found at perldoc.perl.org.

Here is our local, out-dated (pre-5.6) version:

Two common misconceptions are that  is a synonym for s+, and that it's the edge between whitespace characters and non-whitespace characters. Neither is correct.  is the place between a w character and a W character (that is,  is the edge of a ``word''). It's a zero-width assertion, just like ^, $, and all the other anchors, so it doesn't consume any characters. the perlre manpage describes the behaviour of all the regexp metacharacters.

Here are examples of the incorrect application of , with fixes:

    "two words" =~ /(w+)(w+)/;          # WRONG
    "two words" =~ /(w+)s+(w+)/;         # right

    " =matchless= text" =~ /=(w+)=/;   # WRONG
    " =matchless= text" =~ /=(w+)=/;       # right

Although they may not do what you thought they did,  and B can still be quite useful. For an example of the correct use of , see the example of matching duplicate words over multiple lines.

An example of using B is the pattern BisB. This will find occurrences of ``is'' on the insides of words only, as in ``thistle'', but not ``this'' or ``island''.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2025-03-27 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (70 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.