15

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\\)"
nosefrog
  • 795
  • 6
  • 9
  • 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 09 '15 at 05:58
  • 2
    I've made a [patch](http://comments.gmane.org/gmane.emacs.devel/152132) to support raw string literals but the devs are not interested. – knarf Feb 09 '15 at 10:28
  • 1
    You can always tweak how things are displayed, as in this blog post by @abo-abo: http://oremacs.com/2015/01/11/pretty-elisp-regex/ – glucas Feb 09 '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 00:18

2 Answers2

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 ?:)))))))
glucas
  • 20,175
  • 1
  • 51
  • 83
  • 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 09 '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](http://www.ccs.neu.edu/home/shivers/papers/sre.txt) by itself makes elisp a well-worthy substitute for perl python, ruby, etc. Thank you. – gsl Apr 10 '16 at 07:37
8

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:]]+"
Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62
  • That still requires you to double your backslashes. You just don't need quite as many for the average regexp. – cjm May 05 '15 at 16:41