3

测试

(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 参数的值对返回的正则表达式字符串的行为方式没有影响,并且仅出于美观原因?

如果我通过一个包的代码并将 NO-GROUP 从 nil 更改为 t 或在每次出现 rx-to-string 时将其更改为反之亦然,那么假设什么都不会破坏是否安全?

4

1 回答 1

2

No, you cannot assume that nothing will break.

The groups are there not just for cosmetic reasons. These are 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.

This means that the regexps themselves are equivalent, but their combinations are not.

于 2013-09-09T15:07:34.933 回答