in reply to Irregular Expression??

I believe...
$a =~ s/'/q!\'\'!/eg && print "Found a ' in \$a\n";

accomplishes both of your objectives.

Replies are listed 'Best First'.
Re: Re: Irregular Expression??
by Skeeve (Parson) on Feb 24, 2004 at 16:01 UTC
    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.
      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.


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