可能是这个问题真的很困惑,但我被卡住了。如何cl-ppcre:regex-replace-all
替换反斜杠?
例如,我只想转义一些字符,如 '" ( ) 等,所以我将首先使用 | 替换,以查看匹配是否正常:
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\)
打印?谢谢。