substitution woes

N

Nun

I want to use substitution to change a variable which contains

tubing'A'style

so that it instead contains

tubing\'A\'style

I've come very close using:

$COMMENT=~ s/\'/\\\\\'/g;

but that gives me

tubing\\'A\\'style

I've tried many ways but can't seem to find the proper syntax. Can
someone assist? I'd be very grateful :)

DB
 
N

Nun

Perl is not shell. Adding more layers of backslashes randomly is rarely
helpful.

You want to replace ' with \'. The LHS of an s/// is a regex; ' is not
special in regexes; so it doesn't need escaping. The RHS is a
double-quoted string; ' is not special in a double-quoted string but \
is; so you need to escape the \ but not the '.

$COMMENT =~ s/'/\\'/g;

Ben

Thank you for the explanation :)

DB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,715
Latest member
ReeceTaren

Latest Threads

Top