: Learning GNU Emacs, 3rd Edition

3.2.6 Regular Expressions for Search and Replacement Operations

3.2.6 Regular Expressions for Search and Replacement Operations

Sometimes none of the simpler searches described in this chapter are adequate. Regular expressions allow you to build searches with strings that contain various wildcards.

Table 3-4 shows some of the characters you can use in creating a regular expression.

Table3-4.Characters for creating regular expressions

Character(s) Match
^ Matches the beginning of a line.
$ Matches the end of a line.
. Matches any single character (like ? in filenames).
.* Matches any group of zero or more characters (. matches any character and * matches zero or more of the previous character).
< Matches the beginning of a word.
> Matches the end of a word.
[ ] Matches any character specified within the brackets; for example, [a-z] matches any alphabetic character.
s, S Matches any whitespace character: space, a newline, a tab, a carriage return, a formfeed, or a backspace; S matches any character except whitespace.
d, D Matches any single digit, 0-9; D matches any character but a digit.
w, W Matches any "word" character (upper- and lowercase letters, digits, and the underscore character); W matches any character but these.

If you do a regular expression search for ^word$, you would find instances of word on a line by itself. The ^ says that the w must be the first character on the line, the $ says that the d must be the last character.

If you wanted to find all words starting with beg and ending with the letter s, you could use beg[a-z]*s as your regular expression. This would find the words begins, begets, and begonias, in addition to really odd words like shibegrees and altbegaslia. If you don't want these mutantsthat is, if you really want words that begin with beg and end with s, use <beg[a-z]*s>. The < is a special sequence that matches the beginning of a word; > matches the end of a word. If you wanted to find the words beg, big, and bag; but not begonias, and certainly not any strange words with beg on the inside, you would use <b[a-z]g> as the regular expression.

To search for a ^, $, ., *, [, ], or any number of other special characters, you obviously can't use the character itself. Put a backslash () firsti.e., to search for a period, search for . For example, to search for the electronic mail address`:


the regular expression would be:


This is a barebones introduction to regular expressions; see Chapter 11 for more details and Mastering Regular Expressions by Jeffrey Friedl (O'Reilly) for a book-length treatment of this topic.

You can use regular expressions in incremental searches and in query-replace. Table 3-5 lists the commands you use for regular expression searches. Although they are initiated with slightly different commands, the searches are the same as those described earlier in this chapter.

Table3-5.Regular expression search commands

Keystrokes Command name Action
C-M-s Enter Edit ? Search ? Regexp Forward re-search-forward Search for a regular expression forward.
C-M-r Enter Edit ? Search ? Regexp Backwards re-search-backward Search for a regular expression backward.
C-M-s Edit ? Search ? Incremental Search ? Forward Regexp isearch-forward-regexp Search incrementally forward for a regular expression.
C-M-r Edit ? Search ? Incremental Search ? Backward Regexp isearch-backward-regexp Search incrementally backward for a regular expression.
C-M-% Edit ? Replace ? Replace Regexp query-replace-regexp Query-replace a regular expression.
(none) replace-regexp Globally replace a regular expression unconditionally (use with caution).

: 0.447. /Cache: 3 / 1