Elisp: Regex Backslash in Lisp Code
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.
"\n"- Newline.
"\t"- Tab.
"\""- Literal double quote.
"[chars]"- Any of chars
"[\t\n ]+"- Sequence of {tab, newline, space}.
"\\[abc\\]"- Literal square bracket with abc inside.
"(abc)"- Literal parenthesis and text.
"\\(pattern\\)"- Capture pattern.
"\\1"- First captured pattern. Used in replacement.
"\\2"- Second captured pattern. Used in replacement.
- When a file is opened in Emacs, newline is always
\n, regardless whether your file is from {Unix, Windows, Mac}.
Example: Quoting Regex in Emacs Lisp Code
Here's example, suppose you have this text:
src="cat.jpg"
When you call a command such as
list-matching-lines
, you can type the regex in the prompt. Example:
src="\([^"]+?\)"
But in lisp code, the same regex needs to have many backslash escapes, like this:
(re-search-forward "src=\"\\([^\"]+?\\)\"" )
emacs and elisp regex
- Emacs: Regular Expression
- Emacs: List Matching Lines
- Emacs: Regular Expression Syntax
- Emacs: Regex Backslash in Command Prompt
- Emacs: Case Sensitivity in Text Search Commands
- Emacs: Insert Tab or Newline
- Emacs: Wildcards vs Regular Expression
- Elisp: Regular Expression
- Elisp: Regex Functions
- Elisp: Regex Backslash in Lisp Code
- Elisp: Case Sensitivity (case-fold-search)
- Elisp: Find Replace Text in Buffer
- Elisp: Match Data (Regex Result)
- Elisp: Unicode Escape Sequence
- Elisp: Convert Regex to Lisp Regex String
- Elisp: How to Test Regex
- Elisp: Regular Expression in Lisp Syntax, Rx (Package)
- Elisp: Regex Named Character Class and Syntax Table
- Emacs Regex vs Regex in Python, JavaScript, Java