in reply to Re: regexp help -- word boundaries
in thread regexp help -- word boundaries

You and sh1tn both made a fairly common mistake on this type of problem -- of the possibilities, you made one item required, and one item optional. So, instead of matching words in the middle of a longer string of words, it'll now only match single word strings, optionally padded with spaces.

TedPride had the method that I'd typically use, but it'll actually match much more than what water originally asked for. (it'll match if $a is near punctuation, which may result in substituting out part of a hyphenated compound word)

To match exactly what was asked for, I'd have used the same logic as hv's first response. (I've never been comfortable with look(ahead|behind) assertions). If I needed to do multiple replacements, I'd get around his mentioned problem this method by using \G :

$n =~ s/(^|\G| )$a($| )/$1$b$2/ig;