3

(rx bos (1+ (in "0-2")) eos) evaluates to "\\`[0-2]+\\'", so I expected these two functions to be equivalent:

(defun with-rx (s)
  (pcase s
    ((rx bos (1+ (in "0-2")) eos) "matched")
    (t "not matched")))

(defun with-regex (s)
  (pcase s
    ("\\`[0-2]+\\'" "matched")
    (t "not matched")))

However, it seems that pcase treats the rx form as a real regexp in the first case and the string as a simple string in the second case:

ELISP> (equal (rx bos (1+ (in "0-2")) eos) "\\`[0-2]+\\'")
t
ELISP> (with-rx "01210")
"matched"
ELISP> (with-regex "01210")
"not matched"

How come in the rx case, pcase is smart enough to know that it should treat the rx form as a regex?

Drew
  • 75,699
  • 9
  • 109
  • 225
little-dude
  • 177
  • 6

1 Answers1

3

The rx pattern is provided by rx.el using pcase-defmacro (see code) . See also (elisp) Extending pcase on how to add new patterns to the pcase macro.

xuchunyang
  • 14,302
  • 1
  • 18
  • 39