Sams Teach Yourself Emacs in 24 Hours |
|||||||||||||||||
Hour 9: Regular Expressions |
|||||||||||||||||
|
You have now learned everything about matching text with regular expressions, so now look at how the regular expression search mechanisms in Emacs actually work.
There are two functions for regular expression searches in Emacs. An incremental one and an nonincremental one. The incremental one is bound to C-S-s (isearch-forward-regexp), and the nonincremental one is bound to C-S-s RET.
There is nothing special about these two functions except that they use regular expression in the search string. There is, however, one feature in incremental regular expression searches that you must be aware of. When you search incrementally, Emacs moves forward to a location that matches your search. (This is known behavior from ordinary incremental searches). In regular expression incremental searches Emacs can, however, suddenly move back, if there is an earlier location in the buffer which matches the text.
This task shows you how a regular expression incremental search can match some text that is prior to the current match. Follow these steps:
1. Go to the beginning of the buffer and press M-C-s (isearch-forward-regexp). This starts a regular expression incremental search. Now type text. This makes Emacs move forward to the first match of this word (see Figure 9.4).
2. Now continue the regular expression by typing \|. This makes Emacs move back to the beginning of the search, because it concludes that whatever you type after the \| might match text from the beginning (see Figure 9.5).
3. Finally type ot. Emacs moves to the first word, which matches the regular expression text\|ot (see Figure 9.6). This point is in fact prior to the match text, which it moved to before you typed the alternative character in the regular expression.
If you press C-s while you are typing text for the regular expression search, Emacs moves to the next location that matches the regular expression typed so far, which is similar to its behavior in ordinary incremental searches. This has the side effect, however, that the point where you type C-s is marked as the start of the search. This affects the matches found if you later press \| (that is, insert an alternative).
Hour 7 describes how you can search for words only, making Emacs ignore special symbols such as commas, exclamation marks, and so on. This function also has the feature that it treats any number of spaces, tab spaces, and line breaks as one white space. This had the advantage of enabling you to search for marked-up text that you have a copy of on paper. (That is, you might not know where line breaks are located in the binary version of your document.)
Unfortunately, this solution also has the drawback that it is not incremental. You can search incrementally with a regular expression search if you simply insert \< and \> around the word you are searching for and \W in between.
Yet another alternative is to set the variable search-whitespace-regexp to a regular expression that matches what you would expect to be white space.
This can be obtained by inserting the following into your .emacs file.
(setq search-whitespace-regexp "[ \t\r\n]+")
With the preceding in your .emacs
file you can obtain an incremental search which sees any number of white spaces,
tab spaces, and line breaks as a simple white space. Thus you can use C-S-s
(isearch-forward-regexp) as an ordinary search, with two exceptions: You need
to escape the special regular expression symbols, and you do not need to care
about how the space which you see in between
the words is actually represented in your text.
Tip - If you get so fond of using regular expression search that you use it more often than ordinary search, you can make it more accessible by shifting the meaning of C-s and C-S-s and, likewise, for C-r and C-S-r. Do this by inserting the following into your .emacs file: ;;; shift the meaning of C-s and C-M-s |
Sams Teach Yourself Emacs in 24 Hours |
|||||||||||||||||
Hour 9: Regular Expressions |
|||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.