in reply to Re: Irregular Expression??
in thread Irregular Expression??

Why do you evaluate (s/.../e)?
$a =~ s/'/\\'\\'/g && print "Found a ' in \$a\n";
will do it as well - even better, since it doesn't need to evaluate.

Replies are listed 'Best First'.
Re: Re: Re: Irregular Expression??
by delirium (Chaplain) on Feb 24, 2004 at 17:45 UTC
    That's fine if you're used to reading it. In cases where there are lots of backslashes and single-quotes, I think it's less ambiguous to quote with q!..!; I could have also done something like

    $replace = q!\'\'!; s/'/$replace/g;

    ...but I'm a sucker for a one-line solution.
      A one-liner that uses only one backslash:
      s/(')/\Q$1$1/g; # Or s/'/\Q''/g;
      Without backslashes:
      s/(')/quotemeta "$1$1"/eg;

      Abigail

        Normally you correct me. This time I have to tell you, you're wrong - sorry.

        The OP really wanted \'\' to appear for every '!

        So you're missing the \-part too.
      Normally you correct me. This time I have to tell you, you're wrong - sorry.

      The OP really wanted \'\' to appear for every '!

      So you're missing the \-part too.


      Update: Sorry... Wrong recipient. Could someone please delete this node?