in reply to Re: Re: Turn Hate Mail to Love Letter: Regex Multi-Word/Phase Replace
in thread Turn Hate Mail to Love Letter: Regex Multi-Word/Phase Replace

Good point regarding my example.

I should have used $2 instead. But I don't think this is a problem. You just have to remember to add 1 to your $n.

Otherwise you could go the $&-way:

s/$profanity/eval '"'.$profane{$&}.'"'/ge;
  • Comment on Re: Re: Re: Turn Hate Mail to Love Letter: Regex Multi-Word/Phase Replace
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: Turn Hate Mail to Love Letter: Regex Multi-Word/Phase Replace
by chunlou (Curate) on Jun 26, 2003 at 06:40 UTC
    $& is unnecessary. $1 is fine.

    The $n in the key, say, 'lift $n up', is not static in a sense that if someone adds a new key/value pair, the $n will have to be changed. And it's not incremental by 1. It depends on how many bracketing constructs in total in the search pattern.

    But your answer is in the right direction I think.

    Thanks.
      AHHH! Finally I got the problem!

      I forgot that you join all your keys to one regexp!

      I have to admit that I never used Tie:RegexHash. So I have no experience with it. Tell us when you finally had success.

        Regexp::Subst::Parallel seems to do the trick.
        use Regexp::Subst::Parallel; $_ = "Man Honoured at Harbour Centre by the Lift\n"; print; $_ = subst($_, qr/\b(h)arbour\b/i => '$1arbor', qr/\b(h)onour(.*?)\b/i => '$1onor$2', qr/\b(c)entre\b/i => '$1enter', qr/\b(L|l)ift\b/ => sub{$_=$_[1]=~/L/?"E":"e";$_."levato +r"} ); print; __END__ Man Honoured at Harbour Centre by the Lift Man Honored at Harbor Center by the Elevator