Replace Emacs如何删除除“某些文本”之外的所有文本?

Replace Emacs如何删除除“某些文本”之外的所有文本?,replace,emacs,Replace,Emacs,Windows 10 64位,Emacs 25.1 假设我有文本: MyActivity.java:124: RestClient().getAuth("api/method1" + Integer.toString(id) + ".json") MyActivity2.java:224 RestClient().getAuth("api/method2" + Integer.toString(id) + ".json") MyActivity2.java:224:

Windows 10 64位,Emacs 25.1

假设我有文本:

    MyActivity.java:124:       RestClient().getAuth("api/method1" + Integer.toString(id) + ".json")
MyActivity2.java:224    RestClient().getAuth("api/method2" + Integer.toString(id) + ".json")
MyActivity2.java:224:      RestClient().getAuth("api/method2" + Integer.toString(id) + ".json")
MyFragment.java:684:    RestClient().getAuth("api/method2" + Integer.toString(id) + ".json")
MyActivity2.java:224:        RestClient().getAuth("api/method1" + Float.toString(id) + ".xml")
Fragment.java:234:    RestClient().getAuth("api/method1" + Integer.toString(id) + ".json")
MyActivity2.java:224:        RestClient().getAuth("api/method4" + Float.toString(id) + ".xml")
如何删除除api/xxx之外的所有文本。因此,结果必须是:

api/方法1 api/方法2 api/方法2 api/方法2 api/方法1 api/方法1 api/方法4


我希望使用Emacs的内置功能,而无需编写自定义elisp脚本或Emacs宏。

选择要修改的行并运行M-xreplace-regexp。对于replace regexp和replace Use\1,使用。*\api/+?\.*

这个正则表达式说:

.*-匹配任意字符的任意数字 \…保存两个参数之间的位 api/?-一个文字api/后跟下一个之前(包括下一个)的任何内容。 .*-后跟任意字符的任意数字 并将其替换为\1,即上面第一个也是唯一一个\\集合中的文本

输出是预期的:

"api/method1"
"api/method2"
"api/method2"
"api/method2"
"api/method1"
"api/method1"
"api/method4"

选择要修改的行并运行M-xreplace-regexp。对于replace regexp和replace Use\1,使用。*\api/+?\.*

这个正则表达式说:

.*-匹配任意字符的任意数字 \…保存两个参数之间的位 api/?-一个文字api/后跟下一个之前(包括下一个)的任何内容。 .*-后跟任意字符的任意数字 并将其替换为\1,即上面第一个也是唯一一个\\集合中的文本

输出是预期的:

"api/method1"
"api/method2"
"api/method2"
"api/method2"
"api/method1"
"api/method1"
"api/method4"

如果您对在单独的缓冲区中输出结果感到满意,则可以使用如下方法:


C-uM-so api/?RET

如果您对结果输出到单独的缓冲区感到满意,您可以使用如下方式:

C-uM-so api/?RET