[TriLUG] Perl substitution problem

Wes Garrison via TriLUG trilug at trilug.org
Fri Jun 16 12:57:36 EDT 2017


My Google-fu is not good enough to solve this simple problem.

I am building some simple JSON structures by hand in Perl, and I need to
escape double-quotes.

my $string1 = 'this "should" work';
$string1 =~ s/"/\\"/g;
print $string1 \n;
this \"should\" work

^ Yay!  it substituted correctly for both double-quotes!

my $string2 = 'this "should"*,* work';
$string2 =~ s/"/\\"/g;
print $string2 \n;
this \"should" work

^ the comma after the second double-quote broke the substitution. (and it
breaks my JSON)

Why?  What the heck am I missing?

-Wes
_________________________________
Wesley S. Garrison
Network Engineer
Xitech Communications, Inc.
phone:  (919) 260-0803
fax:       (919) 932-5051
__________________________________
"Lead us not into temptation, but deliver us from email."


More information about the TriLUG mailing list