[cl-ppcre-devel] Question about a strange behavior of cl-ppcre

Edi Weitz edi at agharta.de
Fri Aug 15 14:29:49 UTC 2008


On Fri, 15 Aug 2008 16:50:43 +0900, 張 漢秀 <chang at saitama-med.ac.jp> wrote:

> CL-USER> (format t "~A" (cl-ppcre:regex-replace "a" "a" "\\"))
> \
> CL-USER> (format t "~A" (cl-ppcre:regex-replace "a" "a" "\\\\"))
> \
> CL-USER> (format t "~A" (cl-ppcre:regex-replace "a" "a" "\\\\\\\\"))
> \\

The backslash in the replacement specification is special - it can be
followed by things like #\& or #\` to denote specific parts of the
target string - see documentation.  So, if you just want to have a
backslash, you need two backslashes in order to avoid confusion:

  CL-USER 1 > (ppcre:regex-replace "a" "xay" "\\&\\&")
  "xaay"
  T

  CL-USER 2 > (ppcre:regex-replace "a" "xay" "\\&\\\\&")
  "xa\\&y"
  T

Your second example is one (escaped) backslash, your third example
consists of two (escaped) backslashes.  This is conforming with Perl:

  edi at miles:~$ perl -le '$_ = "a"; s/a/\\/; print'
  \
  edi at miles:~$ perl -le '$_ = "a"; s/a/\\\\/; print'
  \\

In your first example, there's only one backslash, but as there's
nothing following it, the parser figured out that you probably meant a
backslash.  This is some kind of a DWIM behaviour and you can of
course argue if it's a good thing or not.

HTH,
Edi.



More information about the Cl-ppcre-devel mailing list