GNU Emacs Manual - Regexp Search

Go to the first, previous, next, last section, table of contents.

## Regular Expression Search

A regular expression (regexp, for short) is a pattern that denotes a class of alternative strings to match, possibly infinitely many. In GNU Emacs, you can search for the next match for a regexp either incrementally or not.

Incremental search for a regexp is done by typing `C-M-s` (`isearch-forward-regexp`). This command reads a search string incrementally just like `C-s`, but it treats the search string as a regexp rather than looking for an exact match against the text in the buffer. Each time you add text to the search string, you make the regexp longer, and the new regexp is searched for. Invoking `C-s` with a prefix argument (its value does not matter) is another way to do a forward incremental regexp search. To search backward for a regexp, use `C-M-r` (`isearch-backward-regexp`), or `C-r` with a prefix argument.

All of the control characters that do special things within an ordinary incremental search have the same function in incremental regexp search. Typing `C-s` or `C-r` immediately after starting the search retrieves the last incremental search regexp used; that is to say, incremental regexp and non-regexp searches have independent defaults. They also have separate search rings that you can access with `M-p` and `M-n`.

If you type `SPC` in incremental regexp search, it matches any sequence of whitespace characters, including newlines. If you want to match just a space, type `C-q `SPC``.

Note that adding characters to the regexp in an incremental regexp search can make the cursor move back and start again. For example, if you have searched for ``foo'` and you add ``\|bar'`, the cursor backs up in case the first ``bar'` precedes the first ``foo'`.

Nonincremental search for a regexp is done by the functions `re-search-forward` and `re-search-backward`. You can invoke these with `M-x`, or bind them to keys, or invoke them by way of incremental regexp search with `C-M-s `RET`` and `C-M-r `RET``.

If you use the incremental regexp search commands with a prefix argument, they perform ordinary string search, like `isearch-forward` and `isearch-backward`. See section Incremental Search.

Go to the first, previous, next, last section, table of contents.