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\\)"
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\\)"
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 ?:)))))))
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:]]+"