Regexp Search Search Search Case
Regular expressions have a syntax in which a few characters are
special constructs and the rest are ordinary. An ordinary
character is a simple regular expression which matches that same
character and nothing else. The special characters are `$
',
`^
', `.
', `*
', `+
', `?
', `[
', `]
' and
`\
'. Any other character appearing in a regular expression is
ordinary, unless a `\
' precedes it.
For example, `f
' is not a special character, so it is ordinary, and
therefore `f
' is a regular expression that matches the string
`f
' and no other string. (It does not match the string
`ff
'.) Likewise, `o
' is a regular expression that matches
only `o
'. (When case distinctions are being ignored, these regexps
also match `F
' and `O
', but we consider this a generalization
of ``the same string,'' rather than an exception.)
Any two regular expressions a and b can be concatenated. The result is a regular expression which matches a string if a matches some amount of the beginning of that string and b matches the rest of the string.
As a simple example, we can concatenate the regular expressions `f
'
and `o
' to get the regular expression `fo
', which matches only
the string `fo
'. Still trivial. To do something nontrivial, you
need to use one of the special characters. Here is a list of them.
. (Period) |
is a special character that matches any single character except a newline.
Using concatenation, we can make regular expressions like ` |
* |
is not a construct by itself; it is a postfix operator that means to
match the preceding regular expression repetitively as many times as
possible. Thus, `
`
The matcher processes a ` |
+ |
is a postfix operator, similar to ` |
? |
is a postfix operator, similar to ` |
[ ... ] |
is a character set, which begins with `
Thus, `
You can also include character ranges in a character set, by writing the
starting and ending characters with a `
Note that the usual regexp special characters are not special inside a
character set. A completely different set of special characters exists
inside character sets: `
To include a `
To include `
When you use a range in case-insensitive search, you should write both
ends of the range in upper case, or both in lower case, or both should
be non-letters. The behavior of a mixed-case range such as ` |
[^ ... ] |
`
`
A complemented character set can match a newline, unless newline is
mentioned as one of the characters not to match. This is in contrast to
the handling of regexps in programs such as |
^ |
is a special character that matches the empty string, but only at the
beginning of a line in the text being matched. Otherwise it fails to
match anything. Thus, ` |
$ |
is similar to ` |
\ |
has two functions: it quotes the special characters (including
`
Because ` |
Note: for historical compatibility, special characters are treated as
ordinary ones if they are in contexts where their special meanings make no
sense. For example, `*foo
' treats `*
' as ordinary since there is
no preceding expression on which the `*
' can act. It is poor practice
to depend on this behavior; it is better to quote the special character anyway,
regardless of where it appears.
For the most part, `\
' followed by any character matches only that
character. However, there are several exceptions: two-character
sequences starting with `\
' that have special meanings. The second
character in the sequence is always an ordinary character when used on
its own. Here is a table of `\
' constructs.
\| |
specifies an alternative. Two regular expressions a and b
with `
Thus, `
`
Full backtracking capability exists to handle multiple uses of ` |
\( ... \) | is a grouping construct that serves three purposes:
This last application is not a consequence of the idea of a
parenthetical grouping; it is a separate feature that is assigned as a
second meaning to the same ` |
\d |
matches the same text that matched the dth occurrence of a
`
After the end of a `
The strings matching the first nine `
For example, `
If a particular ` |
\` | matches the empty string, but only at the beginning of the buffer or string being matched against. |
\' | matches the empty string, but only at the end of the buffer or string being matched against. |
\= | matches the empty string, but only at point. |
\b |
matches the empty string, but only at the beginning or
end of a word. Thus, `
` |
\B | matches the empty string, but not at the beginning or end of a word. |
\< |
matches the empty string, but only at the beginning of a word.
` |
\> |
matches the empty string, but only at the end of a word. ` |
\w | matches any word-constituent character. The syntax table determines which characters these are. See Syntax. |
\W | matches any character that is not a word-constituent. |
\sc |
matches any character whose syntax is c. Here c is a
character that represents a syntax code: thus, ` |
\Sc | matches any character whose syntax is not c. |
The constructs that pertain to words and syntax are controlled by the setting of the syntax table (see Syntax).
Here is a complicated regexp, used by Emacs to recognize the end of a
sentence together with any whitespace that follows. It is given in Lisp
syntax to enable you to distinguish the spaces from the tab characters. In
Lisp syntax, the string constant begins and ends with a double-quote.
`\"
' stands for a double-quote as part of the regexp, `\\
' for a
backslash as part of the regexp, `\t
' for a tab and `\n
' for a
newline.
"[.?!][]\"')]*\\($\\|\t\\| \\)[ \t\n]*"
This contains four parts in succession: a character set matching period,
`?
', or `!
'; a character set matching close-brackets, quotes,
or parentheses, repeated any number of times; an alternative in
backslash-parentheses that matches end-of-line, a tab, or two spaces;
and a character set matching whitespace characters, repeated any number
of times.
To enter the same regexp interactively, you would type TAB
to
enter a tab, and C-j to enter a newline. You would also type
single backslashes as themselves, instead of doubling them for Lisp syntax.