Subject: bug#34641: rx: (or ...) order unpredictable

From: Mattias Engdegård <[email-masked]>
Date: Mon, 25 Feb 2019 10:56:44 +0100

25 feb. 2019 kl. 03.37 skrev Noam Postavsky <[email-masked]>:
> 
> I don't see the problem, isn't "ab?" semantically equivalent to
> "ab\\|a" (and "a\\|ab")?

Good question! When the match is anchored at the end, they are indeed equivalent. They also are equivalent for Posix regexps, which prefer the longest match. But in Emacs, the first (leftmost) matching alternative is used.

Suppose we are matching against the string "abc". Then
ab\|a matches "ab"
a\|ab matches "a"
ab?   matches "ab"
ab??  matches "a" (non-greedy operator)

(I remember writing, young and foolish, [0-9]+\|0[xX][0-9a-fA-F]+ to match a number in decimal or hex, and was surprised that all hex numbers were zero.)

>> (Speaking of regexp-opt, it has another bug that does not affect rx: it returns the empty string if given an empty list of strings. The correct return value is a regexp that never matches anything.
> 
> This sounds familiar, though I can't locate a report for it.

If you do remember, please tell us about it.
The `or' operator in SRE can be used with an empty argument list, and will then not match anything. It is a useful limit case for machine-generated regexps.