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

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.

Replies are listed 'Best First'.
Re: Irregular Expression??
by Abigail-II (Bishop) on Feb 24, 2004 at 17:50 UTC
    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.
        This time I have to tell you, you're wrong - sorry.
        Really? I tested my solution, did you?
        #!/usr/bin/perl use strict; use warnings; my $str = "A'Bcd"; $_ = $str; s/(')/\Q$1$1/g; print "$_\n"; $_ = $str; s/'/\Q''/g; print "$_\n"; $_ = $str; s/(')/quotemeta "$1$1"/eg; print "$_\n"; __END__ A\'\'Bcd A\'\'Bcd A\'\'Bcd
        Seems to print \'\' for every ' to me.

        Abigail

Re: Re: Re: Re: Irregular Expression??
by Skeeve (Parson) on Feb 26, 2004 at 08:24 UTC
    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?