Path: utzoo!mnetor!uunet!husc6!bloom-beacon!mit-eddie!ll-xn!ames!pasteur!ucbvax!PEBBLES.BBN.COM!jr
From: jr@PEBBLES.BBN.COM (John Robinson)
Newsgroups: comp.emacs
Subject: Re: Fatal error (4) from replace-regexp
Message-ID: <812.579284911@pebbles>
Date: 10 May 88 16:28:31 GMT
References: <15447@uflorida.cis.ufl.EDU>
Sender: daemon@ucbvax.BERKELEY.EDU
Reply-To: jr@bbn.com
Organization: The Internet
Lines: 39

You didn;t say which version; my tests apply to version 18.50

I couldn't reproduce the crash, but I did get inconsistent results.
the trouble seems to be with trying to insert a match number (\2 in
your example) before it has ever matched anything.  Since I got more
than one result, I suspect it is possible that this causes a null
pointer dereference, or dereference of uninitialized storage.  In
particular, I repeated your experiment by (1) starting a fresh emacs
(with .emacs) and (2) your example exactly.  The results were similar
between these two cases:

>>        M-x replace-regexp RET \(a\)\|\(b\) RET \1Z\2Y RET

For me, this turned "abcd" into "aY2ZYbZcd".  Weird, huh?  Here's how
this happens.  First match is "a".  This is replaced by "aY2Z", where
the "2" should be empty (* but see below) since the second substring
matched nothing.  Next, match the "b", and replace it by "YbZ".  this
is right, as the first substring matched nothing.

* in fairness to emacs, the result of \2 is never defined in the
documentation (in info) of regular expressions.  It seems that the
matches ought to be preset to empty strings, but apparently they
are preset to the corresponding numbers.

At any rate, the replace doesn't have the intended effect as you can
see.  The \1 and \2 in the replacement do not act as conditionals as
you wanted.  regexp-replace cannot really do the function of tr in a
straightforward way.  The best way (in my book) to use emacs and Unix
is to call tr on the thing you are interested in (surprised?).  For
example, consider:

 (defun rot13-region ()
   (interactive)
   (call-process-region (point) (mark) "tr" t t nil "A-Za-z" "N-ZA-Mn-za-m"))

Why to replicate tr(1) when you can just use it?

/jr
jr@bbn.com or bbn!jr