gpt4 book ai didi

regex - emacs、auctex 和查询用换行符替换字符串

转载 作者:行者123 更新时间:2023-12-01 02:54:53 24 4
gpt4 key购买 nike

我使用 emacs+auctex 和自动填充模式。

现在有时我想搜索(并替换)包含空格的字符串,例如“test1 test2”。问题是,自动填充模式有时会用换行符替换空格字符。因此,搜索和替换“test1 test2”不会找到自动填充用换行符替换空格的字符串。

知道如何解决这个问题吗?

我的文本模式可以在查询替换正则表达式中使用\s-,即“test1\s-test2”,但这在 auctex 模式下不起作用,我不知道为什么。

使用 C-q C-j 使用起来非常不舒服,因为“test1 test2”这样的情况经常发生,尤其是因为我想在一次运行中获得换行符和空格,所以我必须做这样的事情:

M-x query-replace-regexp RET

test1[ <-- one space

C-j C-q

]\s-*test2

最后一个\s-* 是因为在 auctex 中可能有缩进。
这似乎不是很优雅。

顺便说一句,如果你想搜索和替换“test1 test2”是很烦人的,每次都要特别处理换行符的情况……

最佳答案

Emacs 也有“类别”,类似于语法类,但更灵活一些。您可以使用 \cX在正则表达式中匹配类别中的字符 X .

这是一个定义“全空白”类别的函数,其中包括空格、换行符、制表符和换页符,您可以在正则表达式中将其引用为 \cs .

(defun define-all-whitespace-category (table)
"Define the 'all-whitespace' category, 's', in the category table TABLE."
;; First, clear out any existing definition for category 's'. Otherwise,
;; define-category throws an error if one calls this function more than once.
(aset (char-table-extra-slot table 0) (- ?s ? ) nil)
;; Define the new category.
(define-category ?s "all whitespace
All whitespace characters, including tab, form feed, and newline"
table)
;; Add characters to it.
(mapc (lambda (c) (modify-category-entry c ?s table))
'(? ?\n ?\f ?\t)))

(define-all-whitespace-category (standard-category-table))

在我看来,auctex-mode 使用标准类别表,因此您应该能够将 query-replace-regexp 与 foo\cs+bar 一起使用。在那种模式下。

为了测试它,你可以指出一些感兴趣的角色并说:
M-: (looking-at "\\cs") RET

这将评估为 t如果点下的字符在全空白类别中。

关于regex - emacs、auctex 和查询用换行符替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3371346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com