in reply to Word boundary '\B' - Question

Neither ' ', '&', nor ';' are word characters, so there is no word boundary between them.

On the other hand, 'a', and 'f' are word characters (and '&' and ';' still aren't), so there are your word boundaries.

You probably want either negative or positive lookahead/lookbehind for (non-)whitespace instead. Negative version:

$str = 'abac &#x0007E;&#x0007E;&#x0007E; afa&#x0007E;&#x0007E;&#x0007E +;f'; $str =~ s|(?<!\s)\&\#x0007E\;\&\#x0007E\;\&\#x0007E\;(?!\s)|~~~|g; print $str;

Positive lookahead/lookbehind won't match at end/beginning of string:

$str = 'abac &#x0007E;&#x0007E;&#x0007E; afa&#x0007E;&#x0007E;&#x0007E +;f'; $str =~ s|(?<=\S)\&\#x0007E\;\&\#x0007E\;\&\#x0007E\;(?=\S)|~~~|g; print $str;

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Replies are listed 'Best First'.
Re^2: Word boundary '\B' - Question
by prasadbabu (Prior) on Aug 21, 2006 at 15:46 UTC

    Sidhekin, thanks for the clarification.

    I was deceived by the follwing line from the Perl in 21 days book.

    /\Bdef\B/ matches cdefg or abcdefghi, but not def, defghi, or abcdef.

    But now when i re-read the documentation i get clear answer.

    From Documentation:

    A word boundary (\b ) is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order)

    Prasad