I would like to replace forward slaches to backslashes in emacs lisp. 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.
For example, if you make a string of length 1 with the backslash character:
(make-string 1 ?\\)
you get the following response (eg in the minibuffer, when you evaluate the above with 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:
(replace-regexp-in-string "/" (regexp-quote "\\\\") "this/is//a/test")
regexp-quote's documentation reads
(regexp-quote string) Return a regexp string which matches exactly string and nothing else.
Don't use emacs but I guess it supports some form of specifying unicode via \\x
eg maybe this works
(replace-regexp-in-string "\x005c" "\x005f" path))
or
(replace-regexp-in-string "\u005c" "\u005f" path))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.