Elisp: Regex Backslash in Lisp Code

By Xah Lee. Date: .

Backslash in Emacs Lisp Regex String

In emacs lisp code, regular expression is a String, thus it follows string syntax. It needs to be enclosed by double quote string delimiters like "this".

In string, backslash is escape character.

Literal double quote.
Any of chars
"[\t\n ]+"
Sequence of {tab, newline, space}.
Literal square bracket with abc inside.
Literal parenthesis and text.
Capture pattern.
First captured pattern. Used in replacement.
Second captured pattern. Used in replacement.

Example: Quoting Regex in Emacs Lisp Code

Here's example, suppose you have this text:


When you call a command such as list-matching-lines , you can type the regex in the prompt. Example:


But in lisp code, the same regex needs to have many backslash escapes, like this:

(re-search-forward "src=\"\\([^\"]+?\\)\"" )

Regular Expressions (ELISP Manual)

Emacs Lisp, Regex in Lisp Code