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?

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.