One thing at a time all the time!

Welcome to Rocteur
Wednesday, April 24 2024 @ 04:07 PM CEST

What is the difference between 1 and $1 with Regexp

There's some confusion about what \1 and $1 mean, since they seem to
be two different names for the same string. Actually they're two very
different things: $1 is a (read-only) variable, holding a string from
a previous, completed, pattern match. But \1 is referring back to the
memory created in the current pattern match, which hasn't yet
completed. (You didn't ask, but that's why backreferences should be
found only in patterns, such as on the "pattern side" of the s///
command; on the "string side", since the pattern match is completed,
\1 isn't correct.)

You can find more about backreferences in the regular expression
manpages, such as perlre and perlrequick.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

FAQ Manager » Perl » What is the difference between \1 and $1 with Regexp