Regular expressions (regexp)
Summary
The commands that use basic and
extended regular expressions are as
follows:
- Basic
- ed, expr, grep, sed, oedit
- Extended
- awk, grep with -E option, sed with the -E option.
Table 1 summarizes
the features that apply to the applicable shell commands.
Notation | awk | ed | grep -E | expr | sed |
---|---|---|---|---|---|
. |
Yes | Yes | Yes | Yes | Yes |
^ |
Yes | Yes | Yes | No | Yes |
$ |
Yes | Yes | Yes | Yes | Yes |
[ ...] |
Yes | Yes | Yes | Yes | Yes |
[ ::] |
Yes | Yes | Yes | Yes | Yes |
re* | Yes | Yes | Yes | Yes | Yes |
re+ | Yes | No | Yes | No | No |
re? | Yes | No | Yes | No | No |
re|re | Yes | No | Yes | No | No |
\d | Yes | Yes | Yes | Yes | Yes |
( ...) |
Yes | No | Yes | No | No |
\( ...\) |
No | Yes | No | Yes | Yes |
\< |
No | No | No | No | No |
\> |
No | No | No | No | No |
\{ \} |
Yes | No | Yes | No | Yes |
Examples
The
following patterns are given as illustrations, along with descriptions
of what they match:
abc
- Matches any line of text containing the three letters
abc
in that order. a.c
- Matches any string beginning with the letter
a
, followed by any character, followed by the letterc
. ^.$
- Matches any line containing exactly one character (the newline is not counted).
a(b*|c*)d
- Matches any string beginning with a letter
a
, followed by either zero or more of the letterb
, or zero or more of the letterc
, followed by the letterd
. .* [a-z]+ .*
- Matches any line containing a word, consisting of lowercase alphabetic characters, delimited by at least one space on each side.
(morty).*\1
morty.*morty
- These expressions both match lines containing at least two occurrences
of the string
morty
. [[:space:][:alnum:]]
- Matches any character that is either a white space character or alphanumeric.