Is there a package that provides a clearer syntax for building complicated regexes in code?

I currently have in my .emacs this regex, which matches various Java package names:


With all the backslashes and parentheses, I think it does what I intend. If I could do something like

(require '?)

(let ((re
           (?-or (?-literal "com.example")
              (?-literal "net.jahuwaldt.plot")
              (?-literal "javax.swing")
              (?-expr (?-literal "java.") (?-or "awt" "util"))
              (?-literal "org.apache"))
           (?-literal ".")
           (?-repeated (?-group "a-z0-9_.")))

that would be a lot clearer to read and edit. And I wouldn't worry that when I add a new value that I was putting it in at the wrong place.

Is there a package that more or less does what my invented ? package does here?

I expect this to be slower than putting the actual regex in, but I usually have this in interactive commands, where I wouldn't notice the difference.


1 Answer 1


I think you are looking for the structured rx notation. Link to the Emacs manual on rx

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