$1 vs \1 in Perl regex substitutions

Posted by Mr Foo Bar on Stack Overflow See other posts from Stack Overflow or by Mr Foo Bar
Published on 2010-06-18T08:44:51Z Indexed on 2010/06/18 8:53 UTC
Read the original article Hit count: 433

Filed under:
|

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_name = "Some::Package::ButNotThis";

$package_name =~ s{^(\w+::\w+)}{$1};

print $package_name; # Some::Package

This following line seems functionally equivalent:

$package_name =~ s{^(\w+::w+)}{\1};

Are there subtle differences between these two statements? Do they behave differently in different versions of Perl?

© Stack Overflow or respective owner

Related posts about regex

Related posts about perl