在 Emacs 中查找并替换整个单词(不是子字符串)

发布于 2024-12-04 01:44:03 字数 333 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一腔孤↑勇 2024-12-11 01:44:03

使用单词边界锚点:\bfoo\b 仅匹配 foo,而不匹配 foobar

然后,正则表达式通常区分大小写。在大多数风格中,您可以通过在正则表达式中添加 (?I)(?-i) 来显式使其区分大小写。但我不确定 Emacs 正则表达式是否可以做到这一点。

我不知道你的第三个问题。对不起。

Use word boundary anchors: \bfoo\b only matches foo, not foobar.

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.

空袭的梦i 2024-12-11 01:44:03

所有其他答案都提供了可用于让 Emacs 执行您想要的操作的信息。但是您也可以推出自己的搜索/替换功能来执行您想要的操作,如下所示:

(defun search-replace-ala-octi (tosearch toreplace)
  (interactive "sSearch for word: \nsReplace with: ")
  (save-excursion
    (goto-char (point-min))
    (let ((case-fold-search nil)
          (count 0))
      (while (re-search-forward (concat "\\b" tosearch "\\b") nil t)
        (setq count (1+ count))
        (replace-match toreplace 'fixedcase 'literal))
      (message "Replaced %s match(es)" count))))

并将其绑定到您想要的任何键...

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:

(defun search-replace-ala-octi (tosearch toreplace)
  (interactive "sSearch for word: \nsReplace with: ")
  (save-excursion
    (goto-char (point-min))
    (let ((case-fold-search nil)
          (count 0))
      (while (re-search-forward (concat "\\b" tosearch "\\b") nil t)
        (setq count (1+ count))
        (replace-match toreplace 'fixedcase 'literal))
      (message "Replaced %s match(es)" count))))

And bind it to whatever key you want...

失退 2024-12-11 01:44:03

首先,您以后可以将多个问题分成单独的问题吗?它可以让其他人更轻松地为您提供帮助,也让将来遇到相同问题的其他用户更容易从过去的答案中获得帮助。

  1. 查看@Tim 的答案

  2. 查看 Ch k M-% 并阅读有关大小写匹配的部分

  3. 在最后一场比赛后执行 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.

  1. See @Tim's answer

  2. See C-h k M-% and read the section about case matching

  3. Doing C-s after the last match will prompt you that you're at the end, hitting C-s again will wrap from the beginning. Read the docs by hitting C-h i then click on * Emacs then hit g Repeat Isearch RET, second to last paragraph.

倥絔 2024-12-11 01:44:03

对于问题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).

苦妄 2024-12-11 01:44:03

关于问题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 that foo\b can match foo_bar in Emacs (of course it cannot match in Perl).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文