Seperate backreference followed by numeric literal in perl regex

后端 未结 2 1374
别那么骄傲
别那么骄傲 2020-12-11 03:51

I found this related question : In perl, backreference in replacement text followed by numerical literal but it seems entirely different. I have a regex like this one

相关标签:
2条回答
  • 2020-12-11 04:19

    Just put braces around the number:

    s/([^0-9])([xy])/${1}1${2}/g
    
    0 讨论(0)
  • 2020-12-11 04:38

    \1 is a regex atom that matches what the first capture captured. It makes no sense to use it in a replacement expression. You want $1.

    $ perl -we'$_="abc"; s/(a)/\1/'
    \1 better written as $1 at -e line 1.
    

    In a string literal (including the replacement expression of a substitution), you can delimit $var using curlies: ${var}. That means you want the following:

    s/([^0-9])([xy])/${1}1$2/g
    

    The following is more efficient (although gives a different answer for xxx):

    s/[^0-9]\K(?=[xy])/1/g
    
    0 讨论(0)
提交回复
热议问题