[英]How to replace backslashes with forward slashes in all #include “…\…”
[英]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.