3

I want to define a new rx to use within another rx definition, like so:

(defconst my-word
    (rx (or word "*")))

(defconst my-regex
    (rx (1+ my-word)))

However, this code returns the following error:

Eager macro-expansion failure: (error "Unknown rx symbol 'my-word'")

The solution, as per rx documentation, is to define your rx with rx-define:

(rx-define my-word
    (or word "*"))

(defconst my-regex
    (rx (1+ my-word)))

I suspect this has something to do with the sequence of how Emacs loads symbols, but I was just curious as to the specifics of this.

2 Answers 2

4

They're just two utterly different things.

(defconst my-word (rx (or word "*"))) stores the evaluated value "[[:word:]]\\|\\*" in the value slot of the my-word symbol.

(rx-define my-word (or word "*")) sets the symbol property rx-definition on the symbol my-word with the value ((or word "*"))

(We can surmise that symbols which are meaningful to rx therefore have a rx-definition property, and are not evaluated by rx as variables.)

3

Try

(defconst my-word
    (rx (or word "*")))

(defconst my-regex
    (rx (1+ (regexp my-word))))

This works since Emacs 27.

1
  • Thanks for that Michael! I didn’t realise I needed to use the regexp symbol. I’m marking @phils’ answer as the accepted answer as it technically answers my question better, but I appreciate the tip. Oct 2 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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