[Chicago-talk] \1 vs $1

Steven Lembark lembark at wrkhors.com
Thu Nov 4 02:25:52 CST 2004

-- Andy_Bach at wiwb.uscourts.gov

> 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?

Forgot your umbrella again?

There are some matches that cannot be made without
the regex self-matching. These are used heavily in
parsing delimeted text where the delimeter cannot
be hard-coded or where iterating the regex.

As a trivial case, you can find a block of text
delimeted by a non-word character in the first
position and replace it and the closing copy of
it with, say, the original text and a tab:


Without the "\1" on the left side you couln't write
a match againsed what had already been found.

Another way to look at it is that "\1" is part of
the initial matching regex, not its outcome, which
are $1 and $2 in this case.

Steven Lembark                                       85-09 90th Street
Workhorse Computing                                Woodhaven, NY 11421
lembark at wrkhors.com                                     1 888 359 3508

More information about the Chicago-talk mailing list