Quote:
[...]
> > Thanks a lot Steven, it worked great.
> > By the way, why does this regex requires $1 and not /1 ?
> > I thought that $1 works only outside of regex.
> $1 is determined at run time. The code which produces the regular
> expression is also evaluated at run time. That's why we use $1
> instead of \1.
> The experts in c.l.p.m. can expand upon that explanation (for my
> benefit as well)...
Hrm, well, expert, no. By saying that you'll scare off everybody who doesn't
consider himself an expert.
Thanks for the compliment anyway. ;)
Anyway, I generally advise $1 over \1 because...
perldoc perlre
---
Warning on \1 vs $1
Some people get too used to writing things like:
$pattern =~ s/(\W)/\\\1/g;
This is grandfathered for the RHS of a substitute to avoid shocking the sed
{*filter*}s, but it's a dirty habit to get into. That's because in PerlThink,
the righthand side of a s/// is a double-quoted string. \1 in the usual
double-quoted string means a control-A. The customary Unix meaning of \1 is
kludged in for s///. However, if you get into the habit of doing that, you
get yourself into trouble if you then add an /e modifier.
s/(\d+)/ \1 + 1 /eg; # causes warning under -w
Or if you try to do
s/(\d+)/\1000/;
You can't disambiguate that by saying \{1}000, whereas you can fix it with
${1}000. The operation of interpolation should not be confused with the
operation of matching a backreference. Certainly they mean two different
things on the left side of the s///.
---
Does that clear things up?
Steffen