in reply to A regex question

If you want the line endings to be preserved, you can just iterate over the lines:
for (@{$msg->body}){ s/\bblah\b/something/g; print; }

It does nearly the same thing as your original loop, but it has a slightly different notion of where a word ends.

If you really want whitespace delimited words you can change the regular expression to read

s/(?<=^| )blah(?= |$)/something/g;

Replies are listed 'Best First'.
Re^2: A regex question
by johngg (Canon) on Jan 16, 2008 at 23:15 UTC
    s/(?<=^| )blah(?= |$)/something/g;

    I may be wrong but I thought look-behinds were fixed-width so the following gives a compilation error.

    $ perl -le ' > $str = q{adfsdheHjdGafefaffJaff}; > $str =~ s{(?<=^|[A-Z])a}{999}g; > print $str;' Variable length lookbehind not implemented in regex; marked by <-- HER +E in m/(?<=^|[A-Z])a <-- HERE / at -e line 3. $

    You could use an alternation of two look-behinds.

    $ perl -le ' > $str = q{adfsdheHjdGafefaffJaff}; > $str =~ s{(?:(?<=^)|(?<=[A-Z]))a}{999}g; > print $str;' 999dfsdheHjdG999fefaffJ999ff $

    I hope this is of interest.

    Cheers,

    JohnGG