测试
(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/