In perl, backreference in replacement text followed by numerical literal

后端 未结 1 730
闹比i
闹比i 2020-12-19 00:55

I\'m having trouble with a backreference in my replacement text that is followed by a literal. I have tried the following:

perl -0pi -e \"s/(foo&         


        
相关标签:
1条回答
  • 2020-12-19 01:29

    Using \1, \2, etc in the replacement expression is wrong. \1 is a regular expression pattern that means "match what the first capture matched" which makes no sense in a replacement expression. Regular expression patterns should not be used outside of regular expressions! $1, $2, etc is what you should be using there.

    After fixing \1, you have

    perl ... -e'... s/.../...$1$varWithLeadingNumber.../ ...'
    

    That said, I think varWithLeadingNumber is supposed to be a shell variable? You shouldn't have any problems if it's a Perl variable. If you're having the shell interpolate varWithLeadingNumber, the problem can be fixed using

    perl ... -e"... s/.../...\${1}${varWithLeadingNumber}.../ ..."
    

    Note that you will have problems if $varWithLeadingNumber contains "$", "@", "\" or "/", so you might want to use a command line argument instead of interpolation.

    perl ... -pe'
       BEGIN { $val = shift; }
       ... s/.../...$1$val.../ ...
    ' "${varWithLeadingNumber}"
    

    You could also use an environment variable.

    export varWithLeadingNumber
    perl ... -pe's/.../...$1$ENV{varWithLeadingNumber}.../'
    

    or

    varWithLeadingNumber=varWithLeadingNumber \
        perl ... -pe's/.../...$1$ENV{varWithLeadingNumber}.../'
    

    If you did have a \1

    s/...\1.../.../
    

    you can avoid the problem a number of ways including

    s/...(?:\1).../.../
    
    0 讨论(0)
提交回复
热议问题