也可以用 rx 宏,通过 S 表达式来构造正则表达式。
那个例子可以写成 (rx "text" (group "pattern"))
为了说明 rx 的用法,下面再给几个出自 rx.el 注释的例子。
表达式 ^;;\\s-*\n\\|^\n
会变成
(rx (or (and line-start ";;" (0+ space) ?\n)
(and line-start ?\n)))
表达式 [ \t\n]*:\\([^:]+\\|$\\)
会变成
(rx (and (zero-or-more (in " \t\n")) ":"
(submatch (or line-end (one-or-more (not (any ?:)))))))
从英语翻译·查看原文You could build the expressions as sexps with the rx macro instead.
That example would read (rx "text" (group "pattern"))
To illustrate how rx behaves, these additional examples come from rx.el's commentary:
The expression ^;;\\s-*\n\\|^\n
turns into
(rx (or (and line-start ";;" (0+ space) ?\n)
(and line-start ?\n)))
The expression [ \t\n]*:\\([^:]+\\|$\\)
turns into
(rx (and (zero-or-more (in " \t\n")) ":"
(submatch (or line-end (one-or-more (not (any ?:)))))))