繁体   English   中英

如何在Emacs Lisp中用字符串中的反斜杠替换正斜杠?

[英]How to replace forward slashes with backslashes in a string in Emacs Lisp?

我想在emacs lisp中将前向slaches替换为反斜杠。 如果我用这个:

(replace-regexp-in-string "\/" "\\" path))

我收到一个错误。

(error "Invalid use of `\\' in replacement text")

那么如何在第二个正则表达式中表示反斜杠?

您在"C:\\\\foo\\\\bar"中看到的是字符串"C:\\foo\\bar"的文本表示,其中包含用于进一步处理的转义反斜杠。

例如,如果使用反斜杠字符创建长度为1的字符串:

(make-string 1 ?\\)

您得到以下响应(例如,在使用Cx Ce评估上述内容时,在迷你缓冲区中):

"\\"

获得所需内容的另一种方法是切换“文字”标志:

(replace-regexp-in-string "/" "\\" path t t)

顺便说一句,你不需要逃避斜线。

是否需要双重转义?

(replace-regexp-in-string "\/" "\\\\" path)

尝试使用regexp-quote函数,如下所示:

(replace-regexp-in-string "/" (regexp-quote "\\\\") "this/is//a/test")

regexp-quote的文档读取

(regexp-quote string)返回一个与字符串完全匹配的regexp字符串。

不要使用emacs,但我想它支持通过\\ x指定unicode的某种形式

也许这可行

(replace-regexp-in-string "\x005c" "\x005f" path))

要么

(replace-regexp-in-string "\u005c" "\u005f" path))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM