[Chicago-talk] \1 vs $1

Greg Fast gdf at speakeasy.net
Wed Nov 3 14:57:18 CST 2004


On Wed, 3 Nov 2004 12:42:34 -0600, Andy_Bach at wiwb.uscourts.gov wrote:
> I just read a post:
> > ... and \1 is deprecated for $1.
> 
> and having stumbled over this recently, I was a little suprised to hear of 
> the deprecation.  What I'd found was \1 isn't the same as $1, in that
> s/(.)\1//;
> 
> deletes duplicate chars, while;
> s/(.)$1//;
> 
> depends upon the value of $1 - which, in this case'd be set *before* the 
> subst got invoked!  On the right hand side, $1 is what we expect but \1 
> also can change during the course of a regex evaluation - as the 
> parser/matcher works, \1 will take on different values during backtracking 
> etc - though, in the end \1 is the same as $1.
> 
> Er, am I all wet here?

I think you should have read that "...and \1 is deprecated on the right
side of s///".  On the left side (as in any regexp), \1 and $1 differ
as you've described.  But as a holdover from sed, \1 means the same as
$1 on the right side of the subst.

  $a = "abc";
  $a =~ s/(.)b/\1b\1/;

--
Greg Fast
http://cken.chi.groogroo.com/~gdf/


More information about the Chicago-talk mailing list