Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 9: Regular Expressions

Previous HourNext Hour

Sections in this Hour:

 

Hour 9
Regular Expressions

In the shell (also know as the commands prompt) you often use wildcards (also known as patterns). Wildcards are a way of specifying several files in an easy way. Examples of this include

Patterns have some limitations, which made the people behind Emacs choose to use the more-powerful regular expressions. These limitations are not that important in the shell, but they are when you search for text in a buffer, for example. Things which you can't do with patterns but you can do with regular expressions include

Regular Expression Crash Course

In Hours 7, "Searching for Text in a Buffer," and 8, "Searching for Text in Multiple Files," you already saw examples where Emacs wants a regular expression and, believe me, you will see numerous examples in the hours to come. In most cases, however, you only need a very limited subset of the power of regular expressions. Therefore Table 9.1 lists some expressions written both as patterns and as regular expressions. This should give you enough information to read the hours to come.

Table 9.1  Conversion from Patterns to Regular Expression

Pattern

Regular Expression

Comments

file42

file42

This pattern does not contain any of the special characters listed in this table.

file.txt

file\.txt

The dot is one of the special characters; therefore, it must be prefixed with a backslash.

*.txt

.*\.txt

The star represents a number of characters in the pattern. It is replaced with .* in regular expressions. The dot that separates the star and txt is always special for regular expressions, so it must be prefixed with a backslash.

file?.txt

file.\.txt

A question mark represents a single character in the pattern. It is translated to a dot.

file0[1-6].txt

file0[1-6]\.txt

The pattern represents the text file01.txt, file02.txt...file06.txt.

file{AB,CDE,F}

file\(AB\|CDE\|F\)

This pattern represents the text fileAB, fileCDE, or fileF.


In regular expressions, the following characters are special: $, ^, ., *, +, ?, [, ], and \. To include any of these (without their special meanings) in a regular expression, prepend them with a backslash. Thus, to insert the text abc\tdef? as an ordinary string in a regular expression, use the text abc\\tdef\?.

All the regular expression operators are listed in Table 9.2. You will learn more about these operators throughout this hour. So keep reading.

Table 9.2  Regular Expression Symbols

Symbol

Meaning


Repeaters

*

Repeats the previous regular expression any number of times (including zero times).

+

Repeats the previous regular expression any number of times (at least one time).

?

The previous regular expression matches zero or one time.


Grouping / Character Groups

\(...\)

Groups the text for later reference or to overcome precedence problems.

[...]

Matches any of the characters within the group. (But only one character; if you need to match a number, combine this with *, +, or ?.)

[^...]

Matches any character not in this group.


Positions

^

Matches the beginning of the line or string.

$

Matches the end of line or string.

\<

Matches the beginning of a word.

\>

Matches the end of a word.


Special Characters

.

Matches any character.

\w

Matches any character from a word (in most cases, equal to [0-9 a-z A-Z]).

\W

Matches any character not from a word ( [^0-9 a-z A-Z]).


Tip - If you are eager to learn how to use Emacs in your work right away, go ahead and skip the rest of this hour. However, make sure to come back before you finish the book so you know how to use expressions.


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 9: Regular Expressions

Previous HourNext Hour

Sections in this Hour: