[英]How to replace forward slashes with backslashes in a string in Emacs Lisp?
I would like to replace forward slaches to backslashes in emacs lisp. 我想在emacs lisp中将前向slaches替换为反斜杠。 If I use this :
如果我用这个:
(replace-regexp-in-string "\/" "\\" path))
I get an error. 我收到一个错误。
(error "Invalid use of `\\' in replacement text")
So how to represent the backslash in the second regexp? 那么如何在第二个正则表达式中表示反斜杠?
What you are seeing in "C:\\\\foo\\\\bar"
is the textual representation of the string "C:\\foo\\bar"
, with escaped backslashes for further processing. 您在
"C:\\\\foo\\\\bar"
中看到的是字符串"C:\\foo\\bar"
的文本表示,其中包含用于进一步处理的转义反斜杠。
For example, if you make a string of length 1 with the backslash character: 例如,如果使用反斜杠字符创建长度为1的字符串:
(make-string 1 ?\\)
you get the following response (eg in the minibuffer, when you evaluate the above with Cx Ce): 您得到以下响应(例如,在使用Cx Ce评估上述内容时,在迷你缓冲区中):
"\\"
Another way to get what you want is to switch the "literal" flag on: 获得所需内容的另一种方法是切换“文字”标志:
(replace-regexp-in-string "/" "\\" path t t)
By the way, you don't need to escape the slash. 顺便说一句,你不需要逃避斜线。
Does it need to be double-escaped? 是否需要双重转义?
ie 即
(replace-regexp-in-string "\/" "\\\\" path)
Try using the regexp-quote function, like so: 尝试使用regexp-quote函数,如下所示:
(replace-regexp-in-string "/" (regexp-quote "\\\\") "this/is//a/test")
regexp-quote's documentation reads regexp-quote的文档读取
(regexp-quote string) Return a regexp string which matches exactly string and nothing else.
(regexp-quote string)返回一个与字符串完全匹配的regexp字符串。
Don't use emacs but I guess it supports some form of specifying unicode via \\x 不要使用emacs,但我想它支持通过\\ x指定unicode的某种形式
eg maybe this works 也许这可行
(replace-regexp-in-string "\x005c" "\x005f" path))
or 要么
(replace-regexp-in-string "\u005c" "\u005f" path))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.