Emacs - regular expressions in Lisp need to be double-escaped - why?

浪子不回头ぞ 提交于 2019-11-28 08:59:55
scottfrazer

It's because you need to escape backslashes in strings. If you don't escape the backslash of \( in the string, it turns out to be just (

You already have the answer, but a built-in aide for creating regular expressions inside Emacs is re-builder.

M-x re-builder
Kyle Cronin

scottfrazier is correct, one escape is parsed when the string is read, another is parsed when creating the regular expression. It's fairly easy to remember, but it can become a pain, especially when you're trying to match a literal backslash '\'. You end up having to do it four times '\\\\' because you have to double-slash to match the slash in both the initial string parse and the regular expression parse.

And when you write on Stack Overflow about this problem you have to use 8 slashes because markdown uses the slash for an escape character as well.

jrockway

FWIW, emacs-lisp-mode will fontify the special expressions (like \\( and \\) for you. You can then change the faces to be something that stands out.

(They are font-lock-regexp-grouping-construct and font-lock-regexp-grouping-backslash)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!