in reply to string substitution

Here's one that works without capturing parens and backreferences.
my $ariths = '(+*\-/<>=)%^'; $string =~ s/(?:^|(?<=[$ariths]))BAD(?=[$ariths])/HELL/g;
There may be a better way to do it though. Consider \b for word boundries.

Update:Ugg, thanks Not_a_Number... corrected. I used a non-capturing paren when I meant to use a zero-width assertion.


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Replies are listed 'Best First'.
Re: Re: string substitution
by Not_a_Number (Prior) on Oct 08, 2003 at 17:44 UTC

    But that:

    1) removes the arithmetic operators;

    2) doesn't meet the 'start of line' spec.

    :-(

    dave

Re: Re: string substitution
by davido (Cardinal) on Oct 08, 2003 at 18:02 UTC
    Fixed. I shouldn't post Regexp nodes before eating my breakfast cereal.

    For the record:
    1. I mistakenly used (?: ) non-capturing parens when I meant to use (?<= ) and (?= ) zero-width assertions.
    2. I didn't realize that stort meant start.

    The issues have been resolved. :)


    Dave


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein