在 Emacs 中查找并替换整个单词(不是子字符串)
1)在我从 GUI 编辑器调整到 EMACS 的过程中,我似乎很难找到一种明确的方法来为大多数编辑器完成相对简单的任务:查找并替换文本中的整个单词(非子字符串)并找到准确的匹配。 Mx Replace-string 查找子字符串以及整个单词。有正则表达式可以做到这一点吗?
2)另外,有没有办法匹配精确的字符串?例如,如果我想将 myClaSs 替换为每个字母的确切大小写,我需要做什么?现在替换字符串将 myclass 与 myCLasS 匹配。据我所知,只有大小写的第一个字母是匹配的。我假设一些正则表达式也可以解决这个问题。
3)是否可以选择进行环绕搜索?或者我总是必须决定是做 Cs(向前)还是 Cr(向后)
1) In my process of adjusting from my GUI editor to EMACS I seem to have trouble finding a clear-cut way to do a relatively simple task for most editors: Find and replace whole words (non-substrings) within a text and find exact matches. M-x replace-string finds substrings as well as whole words. Is there a regexp that does this?
2) Also, is there a way to match exact strings? For example if I wanted to replace myClaSs with the exact case for each letter what would I have to do? Right now the replace-string matches myclass with myCLasS. As I udnerstand only the first letters in the case are matched. I'm assuming some regexp takes care of this also.
3) Is there an option to do a wrap-around search? Or will I always have to decide whether I do C-s (forward) or C-r (backward)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用单词边界锚点:
\bfoo\b
仅匹配foo
,而不匹配foobar
。然后,正则表达式通常区分大小写。在大多数风格中,您可以通过在正则表达式中添加
(?I)
或(?-i)
来显式使其区分大小写。但我不确定 Emacs 正则表达式是否可以做到这一点。我不知道你的第三个问题。对不起。
Use word boundary anchors:
\bfoo\b
only matchesfoo
, notfoobar
.Then, regular expressions usually are case-sensitive. In most flavors, you can explicitly make them case-sensitive by prepending
(?I)
or(?-i)
to your regex. But I'm not sure if Emacs regexes can do that.I have no idea about your third question. Sorry.
所有其他答案都提供了可用于让 Emacs 执行您想要的操作的信息。但是您也可以推出自己的搜索/替换功能来执行您想要的操作,如下所示:
并将其绑定到您想要的任何键...
All the other answers provide information you can use to get Emacs to do what you want. But you can also roll your own search/replace function to do what you want like so:
And bind it to whatever key you want...
首先,您以后可以将多个问题分成单独的问题吗?它可以让其他人更轻松地为您提供帮助,也让将来遇到相同问题的其他用户更容易从过去的答案中获得帮助。
查看@Tim 的答案
查看
Ch k M-%
并阅读有关大小写匹配的部分在最后一场比赛后执行
Cs
会提示您已结束,点击Cs
again 将从头开始换行。通过点击Ch i
阅读文档,然后点击* Emacs
,然后点击g Repeat Isearch RET
,倒数第二段。Firstly, can you please put multiple questions into separate questions in the future? It makes it easier for others to help you and for other users who have some of the same questions in the future to get help from past answers.
See @Tim's answer
See
C-h k M-%
and read the section about case matchingDoing
C-s
after the last match will prompt you that you're at the end, hittingC-s
again will wrap from the beginning. Read the docs by hittingC-h i
then click on* Emacs
then hitg Repeat Isearch RET
, second to last paragraph.对于问题2,如果搜索字符串全部为小写,则emacs默认进行不区分大小写的搜索。您可以通过将
case-fold-search
设置为 nil 来强制它进行区分大小写的搜索。您可以将其放入 .emacs 文件中:(setq case-fold-search nil)
。For question 2, if the search string is all lower-case, emacs does a case-insensitive search by default. You can force it to do a case-sensitive search by setting
case-fold-search
to nil. You can put this in your .emacs file:(setq case-fold-search nil)
.关于问题1(全词搜索)
@Tim 的答案中的
\b
在遇到字符_
时无法工作。看起来foo\b
在Emacs中可以匹配foo_bar
(当然在Perl中不能匹配)。On question 1 (whole word search)
\b
in @Tim 's answer cannot work when meeting the character_
. It seems thatfoo\b
can matchfoo_bar
in Emacs (of course it cannot match in Perl).