What is the difference between \1 and $1 in a Perl regex?

后端 未结 3 1162
死守一世寂寞
死守一世寂寞 2020-12-01 17:19

What is the difference of doing \\1 as opposed to $1 if any, or are they interchangeable in all situations.

Example:

s/([a-z]+),afklol/$1,bck/;
#agai         


        
3条回答
  •  再見小時候
    2020-12-01 17:34

    They give the same result, but $1 retains its value while \1 does not. Try this:

    s/([a-z]+),afklol/$1,bck/;
    print "\$1 is $1\n";
    #against
    s/([a-z]+),afklol/\1,bck/;
    print "\\1 is \1\n";
    

提交回复
热议问题