in reply to Re: Regex is eating up whitespace
in thread Regex is eating up whitespace

Interesting, I wasn't aware of anchors at all. That definitely makes it easier.

I ended up coming up with this, which seems to work fine and avoids previously tagged cases, but the anchored version is better.

$text =~ s/(\bS\.(?=\s))/\<\1=initial\>/g;

Replies are listed 'Best First'.
Re^3: Regex is eating up whitespace
by GrandFather (Saint) on Sep 29, 2008 at 23:49 UTC

    You should use $1 instead of \1 in the substitution. < and > are not magical in regexen and, apart from $, there are no magical characters in the substitution string in any case - you do not need to escape < and >. Perl allows you to use different delimiters for regexes which can often make the regex much easier to read. Consider:

    $text =~ s!(\bS\.(?=\s))!<$1=initial>!g;

    Perl reduces RSI - it saves typing
Re^3: Regex is eating up whitespace
by dragonchild (Archbishop) on Sep 30, 2008 at 13:18 UTC
    You should definitely read the Regex chapter in the Camel book. It's a tough slog - took me about 10 rereads to finally get it. But, it's extremely worth it. I'm no regex master by any measure, but understanding that chapter went a long way to getting me fluent in Perl.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?