regex - rx-to-string 和 no-group 参数

标签 regex emacs elisp

测试

(dolist (form (list '(+ "a")
                    '(+ "ab")
                    '(or "aa" "bb")
                    '(: bow "a" eow)))
  (dolist (no-group (list nil t))
    (princ (rx-to-string form no-group))
    (terpri))
  (terpri))

输出

\(?:a+\)
a+

\(?:\(?:ab\)+\)
\(?:ab\)+

\(?:\(?:aa\|bb\)\)
\(?:aa\|bb\)

\(?:\<a\>\)
\<a\>

我假设 rx-to-string 中 NO-GROUP 参数的值对返回的正则表达式字符串的行为方式没有影响并且只是出于美观原因,我是否正确?

如果我检查一个包的代码并在每次出现 rx-to-string 时将 NO-GROUP 从 nil 更改为 t 或相反,是否可以安全地假设没有任何东西会中断?

最佳答案

不,您不能假设没有任何东西会损坏。

这些组只是为了美观。这些是 shy groups :

A shy group serves the first two purposes of an ordinary group (controlling the nesting of other operators), but it does not get a number, so you cannot refer back to its value with ‘\digit’. Shy groups are particularly useful for mechanically-constructed regular expressions, because they can be added automatically without altering the numbering of ordinary, non-shy groups.

这意味着正则表达式本身是等价的,但它们的组合不是。

关于regex - rx-to-string 和 no-group 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18692224/

相关文章:

c - 如何将 pcre2 修复为\w 将匹配标记?

ios - 在 Swift 中从字符串中提取某些文本

javascript - 是否有 Vim 正则表达式开始和结束单词原子 "\<"和 "\>"的 JavaScript 等价物?

android - Java代码缩进错误

emacs - 如何修复 emacs 错误 "old-style backquotes detected"

lisp - 在 Emacs Lisp 中获取指向列表元素的指针

Javascript 正则表达式仅匹配数字 1 到 11

emacs - 确定 Emacs 中的行终止符

emacs - common lisp 和 emacs lisp 之间的结构区别

emacs - 在 Emacs SLIME 中关闭调试器