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.