Question

Test

(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))

Output

\(?:a+\)
a+

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

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

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

Am I right in assuming that the value of NO-GROUP argument in rx-to-string makes no difference in how the returned regexp string will behave and that it is only for cosmetic reasons?

If I go through a package's code and changes NO-GROUP from nil to t or vice versa in every occurrence of rx-to-string, is it safe to assume that nothing will break?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top