Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

What does \b{wb} in a regex do?

by nysus (Parson)
on Aug 24, 2020 at 12:40 UTC ( [id://11121029]=perlquestion: print w/replies, xml ) Need Help??

nysus has asked for the wisdom of the Perl Monks concerning the following question:

I'm going through the perl regex tutorial meticulously to try to start filling in the gaps in my regex knowledge and I came across this little nugget: "don't" =~ / .+? \b{wb} /x;  # matches the whole string

I know what the "x" modifier does. But I have no earthly idea what \b{wb} character is or how it works. The tutorial provides no explanation except to say something vague about using it to match apostrophes. So I did a little searching and found this.

But this still sheds no light for me on why throwing this in after ".+?" should cause it to match the whole string "don't". Doesn't ".+?" just match "d"? Why should \b{wb} suck up the rest of the string?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: What does \b{wb} in a regex do?
by Fletch (Bishop) on Aug 24, 2020 at 14:13 UTC

    Regexp::Debugger can be elucidating. And remember ? making something non-greedy changes from "leftmost longest" to "leftmost shortest which still matches"; because the zero-space assertion follows it's got to backtrack and keep expanding until that's satisfied (by a transition from word-char to not-word-char).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: What does \b{wb} in a regex do?
by nysus (Parson) on Aug 24, 2020 at 12:45 UTC

    OK, light bulb finally just went off after staring at this. The not greedy part sucks up the characters until it finds a unicode word boundary.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11121029]
Approved by Corion
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found