14

The sheer number of backslashes my regexps require is pretty crazy. Does elisp have regexp literals, so I can write something like

rx"some\(regexp\)"

instead of

"some\\(regexp\\)"
  • Emacs highlights some regexp constructs like \\|, \\(, and \\). Unfortunately, in many themes this isn't visible. You can override font-lock-regexp-grouping-backslash and font-lock-regexp-grouping-construct to make this more distinct. – Lindydancer Feb 9 '15 at 5:58
  • 2
    I've made a patch to support raw string literals but the devs are not interested. – knarf Feb 9 '15 at 10:28
  • 1
    You can always tweak how things are displayed, as in this blog post by @abo-abo: oremacs.com/2015/01/11/pretty-elisp-regex – glucas Feb 9 '15 at 13:08
  • @knarf it's a shame that Stefan wasn't interested in raw string support, because raw strings would be a complement for a new regexp syntax (which I would be in favor of), not a replacement. – nosefrog Feb 10 '15 at 0:18
13

One option is to use the rx macro to construct your expressions using sexps.

Your example becomes (rx "some" (group "regexp"))

Here are a couple more examples from the commentary section in rx.el, to get an idea of how rx works:

This ^;;\\s-*\n\\|^\n becomes

(rx (or (and line-start ";;" (0+ space) ?\n)
        (and line-start ?\n)))

This [ \t\n]*:\\([^:]+\\|$\\) becomes

(rx (and (zero-or-more (in " \t\n")) ":"
         (submatch (or line-end (one-or-more (not (any ?:)))))))
  • 2
    Thanks for the edit @knarf! I was on my phone and didn't want to put in an example that I might get wrong. :-) – glucas Feb 9 '15 at 13:01
  • We are actually in the process of moving many scripts that use lots of regex search and replace from perl to elisp because of the rx syntax, which makes them easier to maintain. Imho, for similar cases, rx syntax by itself makes elisp a well-worthy substitute for perl python, ruby, etc. Thank you. – gsl Apr 10 '16 at 7:37
7

No it does not have regexp literals, but many find pcre2el to be a helpful alternative.

Specifically using it from elisp like this:

(rxt-pcre-to-elisp "(abc|def)\\w+\\d+")
   ;; => "\\(\\(?:abc\\|def\\)\\)[_[:alnum:]]+[[:digit:]]+"
  • That still requires you to double your backslashes. You just don't need quite as many for the average regexp. – cjm May 5 '15 at 16:41

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.