如何替换“(”with“\(”in the regexp,Emacs / elisp flavor? [英] How to replace "(" with "\(" in the regexp, Emacs/elisp flavor?

查看:304
本文介绍了如何替换“(”with“\(”in the regexp,Emacs / elisp flavor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



更具体而言,我不得不输入 \(等等,每次我想要在Emacs(交互式)正则表达式函数中的括号时(更不用说代码中的 \\(),所以我写了一些类似< ($)

 (defadvice query-replace-regexp(before my-query-replace-regexp activate)
(ad-set-arg 0(replace-regexp-in-string(\\\\((ad-get-arg 0)))
(ad-set-arg 0(replace-regexp-in -string)\\\\)(ad-get-arg 0)))))

希望我可以在交互模式下方便地忘记emacs在正则表达式中的特质,除非我无法获得正则表达式...

 (replace-regexp-in-string(\\\\((abc)

给出 \\(abc 而不是想要的 \(abc 。其他变化的数字因为我开始提问,还可以问另外一个问题:由于lisp代码不应该使用交互式的功能,建议 query-replace-regexp 应该没关系,我是正确的吗?

解决方案

替换你对我有用的。



文本:

 你好,妈妈
嗨儿子!

,并尝试使用query-replace-regexp与您的建议:

  Mx query-replace-regexp(hi)。*(mom)RET \1 \2! RET 

 code>嗨妈妈! 
嗨儿子!

我没有在括号前放一个反斜杠来让他们分组。也就是说,这样就无法匹配实际的括号...



replace-regexp-in-string 产生 \\(abc ,这是一个字符串,相当于交互式输入的 \\(abc 。在一个字符串 \ 中用于表示以下字符是特殊的,例如\ t是一个带有选项卡的字符串,所以为了指定一个反斜杠,你需要在它前面使用一个反斜杠\\ code>是一个包含反斜杠的字符串。



关于建议交互功能,lisp代码可以调用所有需要的交互功能,一个主要的例子是 find-file - 这被称为整个地方,为了使您的建议更安全一点,您可以用 interactive-p 以避免与内部调用混淆:

 (defadvice query-replace-regexp(before my-query-replace-正则表达式t $)
(when(interactive-p)
(ad-set-arg 0(replace-regexp-in-string(\\\\ -arg 0)))
(ad-set-arg 0(replace-regexp-in-string)\\\\)(ad-get-arg 0))) )


Question as title.

More specifically, I'm rather tired of having to type \(, etc. every time I want a parenthesis in Emacs's (interactive) regexp functions (not to mention the \\( in code). So I wrote something like

(defadvice query-replace-regexp (before my-query-replace-regexp activate)
   (ad-set-arg 0 (replace-regexp-in-string "(" "\\\\(" (ad-get-arg 0)))
   (ad-set-arg 0 (replace-regexp-in-string ")" "\\\\)" (ad-get-arg 0)))))

in hope that I can conveniently forget about emacs's idiosyncrasy in regexp during "interaction mode". Except I cannot get the regexp right...

(replace-regexp-in-string "(" "\\\\(" "(abc")

gives \\(abc instead of the wanted \(abc. Other variations on the number of slashes just gives errors. Thoughts?

Since I started questioning, might as well ask another one: since lisp code is not supposed to use interactive functions, advicing query-replace-regexp should be okay, am I correct?

解决方案

The replacement you has works well for me.

Take the text:

hi there mom
hi son!

and try query-replace-regexp with your advice:

M-x query-replace-regexp (hi).*(mom) RET \1 \2! RET

yields

hi mom!
hi son!

I didn't have to put a backslash in front of the parentheses to get them to group. That said, this disables being able to match actual parentheses...

The reason the replace-regexp-in-string yields \\(abc, is that as a string, that is equivalent to an interactively typed \(abc. In a string \ is used to denote that the following character is special, e.g. "\t" is a string with a tab. So, in order to specify just a backslash, you need to use a backslash in front of it "\\" is a string containing a backslash.

Regarding advising interactive functions, lisp code can call interactive functions all it wants. A prime example is find-file - which is called all over the place. To make your advice a little safer, you can wrap the body with a check for interactive-p to avoid mussing with internal calls:

(defadvice query-replace-regexp (before my-query-replace-regexp activate)
  (when (interactive-p)
    (ad-set-arg 0 (replace-regexp-in-string "(" "\\\\(" (ad-get-arg 0)))
    (ad-set-arg 0 (replace-regexp-in-string ")" "\\\\)" (ad-get-arg 0)))))

这篇关于如何替换“(”with“\(”in the regexp,Emacs / elisp flavor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆