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

时间:2022-04-10 21:47:03

I would like to replace forward slaches to backslashes in emacs lisp. If I use this :

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

(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?

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

4 个解决方案

#1


12  

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 (e.g. in the minibuffer, when you evaluate the above with C-x C-e):

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

"\\"

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.

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

#2


6  

Does it need to be double-escaped?

是否需要双重转义?

i.e.

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

#3


2  

Try using the regexp-quote function, like so:

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

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

(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字符串。

#4


0  

Don't use emacs but I guess it supports some form of specifying unicode via \x

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

e.g. maybe this works

例如也许这有效

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

or

要么

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

#1


12  

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 (e.g. in the minibuffer, when you evaluate the above with C-x C-e):

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

"\\"

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.

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

#2


6  

Does it need to be double-escaped?

是否需要双重转义?

i.e.

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

#3


2  

Try using the regexp-quote function, like so:

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

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

(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字符串。

#4


0  

Don't use emacs but I guess it supports some form of specifying unicode via \x

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

e.g. maybe this works

例如也许这有效

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

or

要么

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