Mar 25, 2011, 9:30 AM
Post #1 of 4
Is there a way to use qq() or q() with replacement in regex?
I hope my logic makes sense, here. I have a regex:
If I have to escape quotes or apostrophes, I'd normally do this:
This can become unpleasant looking with multiple escapes:
Is there some means where I can use something similar to qq(...) or q(...) to automatically escape the quotes and apostrophes? If you look at extended pattern modfiers like (?s) or /Q.../E, you can essentially enclose the pattern. I've never used something like /Q.../E for replacement, so I don't know if you can apply those modifiers to the replacement string. But I didn't see a modifier for quotes or apostrophes.
Essentially, I want the regex expression to look like this:
s~foo~qq(bar)q(bar)~g, which would be equivalent to s~foo~\"bar\"\'bar\'~g.
I would appreciate any help. Thank you.