regex - 有没有更好的方法在 Emacs 中进行正则表达式搜索和删除?

标签 regex search emacs replace

在阅读 emacs 帮助和 emacs wiki 时,我没有找到一种明显的方法来搜索正则表达式,然后简单地删除它找到的所有匹配文本。我原本以为我可以使用正则表达式搜索和替换功能 M-x query-replace-regexp,但不知道如何用空字符串替换我的匹配项。在空字符串上按回车键只会退出。增量 reg expr 搜索 (C-M-s) 突出显示的字段不遵循与标记文本 block 相同的规则。否则我会简单地剪掉它(C-w)。

考虑以下场景。我想从包含 3 个或更多零的数字列表中去除尾随零。

0.075000
0.050000
0.10000
0.075000

这就是我解决这个问题的迂回方法。

F3                - begin keyboard macro
C-M-s for 000*    - forward regexpr search
                    match the trailing zeros find the first match
C-<SPC>           - mark the position at the end of the match (after the last 0)
C-M-r for [1-9]   - reverse regexpr search
                    match the reverse non-zero digit, mark is now on the non-zero digit
C-f               - move mark forward one space
C-w               - cut/kill the text
F4                - end keyboard macro, run until list is processed

当然有更好的方法来做到这一点。有什么想法吗?

最佳答案

使用replace-regexp要做到这一点。要从具有 3 个或更多 0 的数字中去除尾随 0:

M-x replace-regexp <RET>000+<RET><RET>

关于regex - 有没有更好的方法在 Emacs 中进行正则表达式搜索和删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23527258/

相关文章:

emacs - 更改 Emacs 的初始化位置

emacs - 我怎样才能在 emacs 中并排执行 "svn diff"

javascript - 我该如何做这个正则表达式?

ruby-on-rails - 如何实现像Stack Overflow一样的搜索

linux - 在文件中搜索模式并输出结果

php - Wordpress 搜索 - 过滤页面和帖子

regex - emacs 正则表达式问题

python - 如何使用正则表达式从字符串中提取前两个字符

正则表达式查找由一两个单词分隔的 2 个单词

c# - 数字的正则表达式