in reply to Re: Regular Expression, substitution
in thread Regular Expression, substitution

\b matches a word boundary. It's a zero width anchor. It works like qr/(?<=\w)(?=\W)|(?<=\W)(?=\w)/ which is likely to blow your mind unless you are comfortable with look around matches.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^3: Regular Expression, substitution
by AnomalousMonk (Archbishop) on Apr 06, 2016 at 05:10 UTC

    Actually, it's trickier than that.  qr/(?<=\w)(?=\W)|(?<=\W)(?=\w)/ requires a character before and after the assertion, whereas  \b can match at the start and end of a string. A kind of double-negative is needed in an equivalent look-around:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'xx-xx'; ;; my $lbw = qr/(?<=\w)(?=\W)|(?<=\W)(?=\w)/; printf qq{$-[0] } while $s =~ m{ $lbw }xmsg; print qq{\n}; ;; my $wb = qr{ (?<!\w)(?!\W) | (?<!\W)(?!\w) }xms; printf qq{$-[0] } while $s =~ m{ $wb }xmsg; print qq{\n}; ;; printf qq{$-[0] } while $s =~ m{ \b }xmsg; " 2 3 0 2 3 5 0 2 3 5

    Update: Now,  \B is another story...


    Give a man a fish:  <%-{-{-{-<

Re^3: Regular Expression, substitution
by marinersk (Priest) on Apr 06, 2016 at 04:47 UTC

    Thanks! I was just reading up on it so I could answer my own question.

    I can't imagine how I've gone decades not having bumped into it.