http://www.perlmonks.org?node_id=1052355


in reply to Regex: Changing $1

Try
open (FH,"test.test") or die; while(<FH>) { if ($_ =~ /"(.+?)"/) { print "Matched: $1\n"; s/$1/New string/; } print; }
P.S. You can make a one liner out of this
If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Replies are listed 'Best First'.
Re^2: Regex: Changing $1
by hdb (Monsignor) on Sep 04, 2013 at 14:39 UTC

    Is problematic if $1 also matches outside of the quotes, e.g. test "test".

Re^2: Regex: Changing $1
by choroba (Cardinal) on Sep 04, 2013 at 14:36 UTC
    Only works if $1 does not contain any special characters. Prepend \Q to "quote" all the special characters in $1.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ