0

I cannot find the resources for this. I want to escape double quotes with perl and i am new to this language.

Here's what I have:

$comment =~ s/\"/\\\"/g;

It doesn't seem to be working. What is a proper solution?

slicks1
  • 349
  • 1
  • 13

2 Answers2

0

One way to insert things that would need escaping, and without counting backslashes

$comment =~ s/"/q(\")/eg;

With /e modifier the replacement side is evaluated as code, and the result used as replacement. See it in perlop and, say, this post. Here this is used to form a string literal with \, by q().

Note though that in this simple case s/"/\"/ works fine. There is probably more going on in your code, or your quotes come already escaped in which case extra \ affect what you see later.

zdim
  • 64,580
  • 5
  • 52
  • 81
0

No counting required:

$comment =~ s.(?=").\\.g;
Pinecone
  • 391
  • 4
  • 9