What is the difference between using $1 vs \1 in Perl regex substitutions?

前端 未结 2 962
囚心锁ツ
囚心锁ツ 2021-01-11 13:57

I\'m debugging some code and wondered if there is any practical difference between $1 and \\1 in Perl regex substitutions

For example:

my $package_n         


        
2条回答
  •  无人及你
    2021-01-11 14:45

    From perldoc perlre:

    The bracketing construct "( ... )" creates capture buffers. To refer to the current contents of a buffer later on, within the same pattern, use \1 for the first, \2 for the second, and so on. Outside the match use "$" instead of "\".

    The \ notation works in certain circumstances outside the match. But it can potentially clash with octal escapes. This happens when the backslash is followed by more than 1 digits.

提交回复
热议问题