15.6 Syntax of Regular Expressions
This section (and this manual in general) describes regular expression features that users typically use. See Regular Expressions in The Emacs Lisp Reference Manual, for additional features used mainly in Lisp programs.
Regular expressions have a syntax in which a few characters are special constructs and the rest are ordinary. An ordinary character matches that same character and nothing else. The special characters are β$^.*+?[\
β. The character β]
β is special if it ends a character alternative (see below). The character β-
β is special inside a character alternative. Any other character appearing in a regular expression is ordinary, unless a β\
β precedes it. (When you use regular expressions in a Lisp program, each β\
β must be doubled, see the example near the end of this section.)
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 trivial example, concatenating the regular expressions βf
β and βo
β gives the regular expression βfo
β, which matches only the string βfo
β. To do something less trivial, 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. For example, the regular expressions βa.b
β matches any three-character string that begins with βa
β and ends with βb
β.
*
is not a construct by itself; it is a postfix operator that means to match the preceding regular expression repetitively any number of times, as many times as possible. Thus, βo*
β matches any number of βo
βs, including no βo
βs.
β*
β always applies to the smallest possible preceding expression. Thus, βfo*
β has a repeating βo
β, not a repeating βfo
β. It matches βf
β, βfo
β, βfoo
β, and so on.
The matcher processes a β*
β construct by matching, immediately, as many repetitions as can be found. Then it continues with the rest of the pattern. If that fails, backtracking occurs, discarding some of the matches of the β*
β-modified construct in case that makes it possible to match the rest of the pattern. For example, in matching βca*ar
β against the string βcaaar
β, the βa*
β first tries to match all three βa
βs; but the rest of the pattern is βar
β and there is only βr
β left to match, so this try fails. The next alternative is for βa*
β to match only two βa
βs. With this choice, the rest of the regexp matches successfully.
+
β
is a postfix operator, similar to β*
β except that it must match the preceding expression at least once. Thus, βca+r
β matches the strings βcar
β and βcaaaar
β but not the string βcr
β, whereas βca*r
β matches all three strings.
?
β
is a postfix operator, similar to β*
β except that it can match the preceding expression either once or not at all. Thus, βca?r
β matches βcar
β or βcr
β, and nothing else.
*?
, +?
, ??
β
are non-greedy variants of the operators above. The normal operators β*
β, β+
β, β?
β match as much as they can, as long as the overall regexp can still match. With a following β?
β, they will match as little as possible.
Thus, both βab*
β and βab*?
β can match the string βa
β and the string βabbbb
β; but if you try to match them both against the text βabbb
β, βab*
β will match it all (the longest valid match), while βab*?
β will match just βa
β (the shortest valid match).
Non-greedy operators match the shortest possible string starting at a given starting point; in a forward search, though, the earliest possible starting point for match is always the one chosen. Thus, if you search for βa.*?$
β against the text βabbab
β followed by a newline, it matches the whole string. Since it can match starting at the first βa
β, it does.
\{n\}
β
is a postfix operator specifying n
repetitionsβthat is, the preceding regular expression must match exactly n
times in a row. For example, βx\{4\}
β matches the string βxxxx
β and nothing else.
\{n,m\}
β
is a postfix operator specifying between n
and m
repetitionsβthat is, the preceding regular expression must match at least n
times, but no more than m
times. If m
is omitted, then there is no upper limit, but the preceding regular expression must match at least n
times.\
β\{0,1\}
β is equivalent to β?
β.\
β\{0,\}
β is equivalent to β*
β.\
β\{1,\}
β is equivalent to β+
β.
[ β¦ ]
β
is a character set, beginning with β[
β and terminated by β]
β.
In the simplest case, the characters between the two brackets are what this set can match. Thus, β[ad]
β matches either one βa
β or one βd
β, and β[ad]*
β matches any string composed of just βa
βs and βd
βs (including the empty string). It follows that βc[ad]*r
β matches βcr
β, βcar
β, βcdr
β, βcaddaar
β, etc.
You can also include character ranges in a character set, by writing the starting and ending characters with a β-
β between them. Thus, β[a-z]
β matches any lower-case ASCII letter. Ranges may be intermixed freely with individual characters, as in β[a-z$%.]
β, which matches any lower-case ASCII letter or β$
β, β%
β or period. As another example, β[Ξ±-ΟΞ―]
β matches all lower-case Greek letters.
You can also include certain special character classes in a character set. A β[:
β and balancing β:]
β enclose a character class inside a character alternative. For instance, β[[:alnum:]]
β matches any letter or digit. See Char Classes in The Emacs Lisp Reference Manual, for a list of character classes.
To include a β]
β in a character set, you must make it the first character. For example, β[]a]
β matches β]
β or βa
β. To include a β-
β, write β-
β as the last character of the set, tho you can also put it first or after a range. Thus, β[]-]
β matches both β]
β and β-
β.
To include β^
β in a set, put it anywhere but at the beginning of the set. (At the beginning, it complements the setβsee below.)
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-z
β is somewhat ill-defined, and it may change in future Emacs versions.
[^ β¦ ]
β
β[^
β begins a complemented character set, which matches any character except the ones specified. Thus, β[^a-z0-9A-Z]
β matches all characters except ASCII letters and digits.
β^
β is not special in a character set unless it is the first character. The character following the β^
β is treated as if it were first (in other words, β-
β and β]
β are not special there).
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 grep
.
^
β
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, β^foo
β matches a βfoo
β that occurs at the beginning of a line.
For historical compatibility reasons, β^
β can be used with this meaning only at the beginning of the regular expression, or after β\(
β or β\|
β.
$
β
is similar to β^
β but matches only at the end of a line. Thus, βx+$
β matches a string of one βx
β or more at the end of a line.
For historical compatibility reasons, β$
β can be used with this meaning only at the end of the regular expression, or before β\)
β or β\|
β.
\
β
has two functions: it quotes the special characters (including β\
β), and it introduces additional special constructs.
Because β\
β quotes special characters, β\$
β is a regular expression that matches only β$
β, and β\[
β is a regular expression that matches only β[
β, and so on.
See the following section for the special constructs that begin with β\
β.
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.
As a β\
β is not special inside a character alternative, it can never remove the special meaning of β-
β or β]
β. So you should not quote these characters when they have no special meaning either. This would not clarify anything, since backslashes can legitimately precede these characters where they have special meaning, as in β[^\]
β ("[^\\]"
for Lisp string syntax), which matches any single character except a backslash.