A regular expression is a pattern that is matched against a subject string from left to right. Most characters are ordinary: they stand for themselves in a pattern, and match the corresponding characters in the subject. As a trivial example, the pattern
The quick brown fox
matches a portion of a subject string that is identical to itself. The power of regular expressions comes from the ability to include alternatives and repetitions in the pattern. These are encoded in the pattern by the use of special characters, which do not stand for themselves but instead are interpreted in some special way. Here is a brief description of regular expression syntax as used in sed.
., a grouped regexp (see below), or a bracket expression. As a GNU extension, a postfixed regular expression can also be followed by
*; for example,
a**is equivalent to
a*. POSIX 1003.1-2001 says that
*stands for itself when it appears at the start of a regular expression or subexpression, but many nonGNU implementations do not support this and portable scripts should instead use
\*in these contexts.
*, but matches one or more. It is a GNU extension.
*, but only matches zero or one. It is a GNU extension.
*, but matches exactly i sequences (i is a decimal integer; for portability, keep it between 0 and 255 inclusive).
\(abcd\)*: this will search for zero or more whole sequences of ‘abcd’, while
abcd*would search for ‘abc’ followed by zero or more occurrences of ‘d’. Note that support for
\(abcd\)*is required by POSIX 1003.1-2001, but many non-GNU implementations do not support it and hence it is not universally portable.
In most scripts, pattern space is initialized to the content of each
line (see How
sed works). So, it is a
useful simplification to think of
^#include as matching only
lines where ‘#include’ is the first thing on line—if there are
spaces before, for example, the match fails. This simplification is
valid as long as the original content of pattern space is not modified,
for example with an
^ acts as a special character only at the beginning of the
regular expression or subexpression (that is, after
\|). Portable scripts should avoid
^ at the beginning of
a subexpression, though, as POSIX allows implementations that
^ as an ordinary character in that context.
$also acts as a special character only at the end of the regular expression or subexpression (that is, before
\|), and its use at the end of a subexpression is not portable.
[aeiou]matches all vowels. A list may include sequences like char1
-char2, which matches any character between (inclusive) char1 and char2.
^ reverses the meaning of list, so that
it matches any single character not in list. To include
] in the list, make it the first character (after
^ if needed), to include
- in the list,
make it the first or last; to include
it after the first character.
are normally not special within list. For example,
matches either ‘\’ or ‘*’, because the
\ is not
special here. However, strings like
[:space:] are special within list and represent collating
symbols, equivalence classes, and character classes, respectively, and
[ is therefore special within list when it is followed by
:. Also, when not in
POSIXLY_CORRECT mode, special escapes like
\t are recognized within list. See Escapes.
$, but less tightly than the other regular expression operators.
\(...\)parenthesized subexpression in the regular expression. This is called a back reference. Subexpressions are implicity numbered by counting occurrences of
^. Note that the only C-like backslash sequences that you can portably assume to be interpreted are
\\; in particular
\tis not portable, and matches a ‘t’ under most implementations of sed, rather than a tab character.
Note that the regular expression matcher is greedy, i.e., matches are attempted from left to right and, if two or more matches are possible starting at the same character, it selects the longest.