Regex cl ppcre:正则表达式替换和替换中的反斜杠

Regex cl ppcre:正则表达式替换和替换中的反斜杠,regex,lisp,common-lisp,cl-ppcre,Regex,Lisp,Common Lisp,Cl Ppcre,也许这个问题真的很愚蠢,但我被卡住了。如何在cl ppcre:regex replace allreplacement中放置反斜杠 例如,我只想转义一些字符,比如“”()等,因此我将首先使用“替换”,以查看匹配是否正确: CL-USER> (princ (cl-ppcre:regex-replace-all "(['\\(\\)\"])" "foo \"bar\" 'baz' (test)" "|\\1")) PRINTED: foo |"bar|" |'baz|' |(t

也许这个问题真的很愚蠢,但我被卡住了。如何在
cl ppcre:regex replace all
replacement中放置反斜杠

例如,我只想转义一些字符,比如“”()等,因此我将首先使用“替换”,以查看匹配是否正确:

    CL-USER> (princ (cl-ppcre:regex-replace-all "(['\\(\\)\"])"
"foo \"bar\" 'baz' (test)" "|\\1"))
    PRINTED: foo |"bar|" |'baz|' |(test|)
好的,让我们把斜杠放进去:

    CL-USER> (princ (cl-ppcre:regex-replace-all "(['\\(\\)\"])"
"foo \"bar\" 'baz' (test)" "\\\1"))
    PRINTED: foo "bar" 'baz' (test) ;; No luck
不,我们需要两条斜线:

    CL-USER> (princ (cl-ppcre:regex-replace-all "(['\\(\\)\"])"
"foo \"bar\" 'baz' (test)" "\\\\1"))
    PRINTED: foo \1bar\1 \1baz\1 \1test\1 ;; Got slash, but not \1
也许是这样

(princ (cl-ppcre:regex-replace-all "(['\\(\\)\"])"
"foo \"bar\" 'baz' (test)" "\\\{1}"))
PRINTED: foo "bar" 'baz' (test) ;; Nope, no luck here
当然,如果我在斜杠之间留出空间,一切都可以,但我不需要它

(princ (cl-ppcre:regex-replace-all "(['\\(\\)\"])"
"foo \"bar\" 'baz' (test)" "\\ \\1"))
PRINTED: foo \ "bar\ " \ 'baz\ ' \ (test\ )
那么,我如何编写才能打印出
foo\“bar\”\'baz\(test\)
?谢谢。

六个源斜杠
在源代码中写入字符串时,每个斜杠都被用作转义符。您希望替换文本为字符序列
\\1
。对替换中的第一个斜杠进行编码(因为CL-PPCRE将处理斜杠),CL-PPCRE需要查看字符序列
\\\1
。前两个斜杠编码斜杠,第三个编码组号。要将该字符序列作为Lisp字符串,您必须编写
“\code>”

延迟回答,但对于其他人,请注意,在这种情况下最好避免使用字符串:

(cl-ppcre:regex-replace-all '(:register (:char-class #\' #\( #\) #\"))
                            "foo \"bar\" 'baz' (test)"
                            '("\\" 0))
(cl-ppcre:regex-replace-all '(:register (:char-class #\' #\( #\) #\"))
                            "foo \"bar\" 'baz' (test)"
                            '("\\" 0))