Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: finding '

by mfriedman (Monk)
on Jun 15, 2002 at 05:21 UTC ( #174804=note: print w/replies, xml ) Need Help??


in reply to finding '

I prefer

$string =~ s/([^\])\'/$1\\\'/g;

Replies are listed 'Best First'.
Re: Re: finding '
by ChemBoy (Priest) on Jun 15, 2002 at 06:28 UTC

    For a different variation on the same theme, try

    $string =~ s/(?<!\\)'/\\'/g;

    Though this raises the question "what if the string contains a backslash followed by a single quote?" Which makes the regex slightly more complicated, and leads back to he conclusion that quotemeta might not be such a bad idea, after all...

    Update: while I'm writing silly regexes...

    $string =~ s/(?<!\\)(?=')/\\/g;

    <duck>



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://174804]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-06-13 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (57 votes). Check out past polls.

    Notices?