Skip to main content

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.