0

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:

"\\(com\\.example\\|net\\|jahuwaldt\\.plot\\|javax\\.swing\\|java\\.\\(awt\\|util\\)\\|org\\.apache\\)\\.[a-z0-9_.]*"

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

(require '?)

(let ((re
        (?-expr
           (?-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.

0

1 Answer 1

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.