在阅读 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/