From: "Dave Storrs" <[EMAIL PROTECTED]>

> Both \1 and $1 refer to what is matched by the first set of parens in a
> regex.  AFAIK, the only difference between these two notation is that \1
> is used within the regex itself and $1 is used outside of the regex.  Is
> there any reason not to standardize these down to one notation (i.e.,
> eliminate one or the other)?

\1 came from sed and friends.  I think an early driving force was
maintaining familiarity with things like awk and sed.  Even today there are
still people that switch to and from other reg-ex languages.  Emacs is the
most common for me (though I still dabble with awk).  I don't see a real
advantage in taking out \1, and it is very likely to needlessly break legacy
code, and additionally confuse various developers that have a habbit of
using \1.

On the other hand, the use of $1with substitutions is important for
consistency.  When you write s/../.../e, you're going to need to use a
substitution variable, "\1" just doesn't fit.
s/(...)/pre\1post/;  works fine
s/(...)/pre$1post/; is the question. I tend to use it only because I
sometimes switch to:
s/(...)/func() . "$1post"/e;  for various reasons..  I just try and
standardize on $1, but that's just me.

Additionally the use of $1 in the matching reg-ex is ambiguous as in:
m/(...).*?$1/;
Does it refer to the internal set of (..), or does it mean the previous
value of $1 before this match.. This becomes non-obvious to the observer in
the following case:
m/($keyword).*?$1/;
Here, our mindset is substitution of external variables, the casual
(non-seasoned) observer might not understand that it really means:
m/($keyword).*?\1/;

My argument is that both \1 and $1 have their places, and limiting to one
type can be troublesome.  Plus, TMTOWTDI. :)

-Michael

Reply via email to