All notes
Regexp

Basics

re-build

Construct a regexp interactively. This command makes the current buffer the "target" buffer of the regexp builder. It displays a buffer named "*RE-Builder*" in another window, initially containing an empty regexp.

As you edit the regexp in the "*RE-Builder*" buffer, the matching parts of the target buffer will be highlighted.


M-x re-builder

FAQ

Why two backslashes are needed?

One escape is parsed when the string is read, another is parsed when creating the regular expression. When you're trying to match a literal backslash '\', you end up having to do it four times '\\\\' because you have to double-slash to match the slash in both the initial string parse and the regular expression parse.


(defun wcfHtmlOccur ()
  "List all html headers in occur buffer."
  (interactive)
  (occur "<h[1-6]>.*</h[1-6]>\\|<article>")
)

The “or” operator is \|. Strings and regexps are orthogonal in Emacs Lisp, so if you put a regexp in a string, you need to write e.g. "True\\|False".

Emacs has 2 escaping styles, one in EmacsLisp the other when used in commands, ie. from the M-x prompt (rgrep, occur, ...)

In EmacsLisp, use the double backslash \\|
From M-x ... use a single backslash \|

As a side note, when writing embedded EmacsLisp, for example in yasnippet dynamic expansions, you have to use a quadruple backslash: \\\\| (to escape the double backslashes.)

Always avoid this (if possible), for example in yasnippet you can provide mode related emacslisp, without the additional escaping, via a .yas-setup.el.

For further details, see:

M-: (info "(elisp) Regexp Special") RET