Ordinary and special characters #
| Pattern | Matches |
|---|---|
cat | The literal text cat |
. | Any single character |
\. | A literal dot |
\\ | A literal backslash |
a\+b | Literal a+b |
Outside brackets these need a backslash to be literal: ^ . [ $ ( ) | * + ? { \.
text
POSIX extended regular expressions as used by grep -E, with notes on where Perl-style engines differ.
Updated
This sheet describes POSIX extended regular expressions (ERE), the flavor selected by grep -E. Unless a card says otherwise, the syntax is portable ERE; Perl-style differences are collected in the last section.
| Pattern | Matches |
|---|---|
cat | The literal text cat |
. | Any single character |
\. | A literal dot |
\\ | A literal backslash |
a\+b | Literal a+b |
Outside brackets these need a backslash to be literal: ^ . [ $ ( ) | * + ? { \.
| Pattern | Matches |
|---|---|
^Error | Error at the start of a line |
;$ | ; at the end of a line |
^$ | An empty line |
^[0-9]+$ | A line made only of digits |
red|green|blue one of three words
gr(a|e)y gray or grey
(ab)+ ab, abab, ababab, ...
^(GET|POST) /api/ request lines for two methods
| has the lowest precedence, so ^a|b$ means ^a or b$; group to change that.
| Piece | Repeats the atom |
|---|---|
x* | Zero or more times |
x+ | One or more times |
x? | Zero or one time |
x{3} | Exactly three times |
x{2,} | Two or more times |
x{2,5} | Two to five times |
A quantifier applies to the single atom before it: a character, a bracket expression or a group.
[0-9]{4}-[0-9]{2}-[0-9]{2} a date such as 2026-10-14
[A-Fa-f0-9]{6} six hex digits
(na){2,} nana, nanana, ...
A { that is not followed by a digit is an ordinary character on macOS. Bounds may not exceed 255 there.
Note
pattern: cat|category
input: category
POSIX: category (longest match at the earliest position)
Perl/JS: cat (first alternative that succeeds)
POSIX engines choose the earliest starting position, then the longest match from there. Backtracking engines stop at the first alternative that works.
| Pattern | Matches one character |
|---|---|
[aeiou] | Any listed vowel |
[^aeiou] | Anything except those |
[a-z] | A range in the collating order |
[]x] | ] or x; put ] first |
[a-] | a or -; put - first or last |
[*.] | A literal star or dot |
Inside brackets, \ and most other special characters are ordinary.
| Class | Members |
|---|---|
[[:digit:]] | Decimal digits |
[[:alpha:]] | Letters |
[[:alnum:]] | Letters and digits |
[[:space:]] | Whitespace, including tab and newline |
[[:upper:]], [[:lower:]] | Upper- or lowercase letters |
[[:punct:]] | Punctuation |
[[:xdigit:]] | Hexadecimal digits |
Classes are locale-aware and only valid inside a bracket expression, so write [[:digit:]], not [:digit:].
Warning
[[:lower:]]+ prefer a class
[a-z]+ may differ between locales
Ranges follow the collating sequence, so prefer named classes in portable patterns.
| Option | Effect |
|---|---|
-E | Use extended (ERE) syntax |
-i | Ignore case |
-v | Select lines that do not match |
-o | Print only the matched parts |
-w, -x | Match whole words or whole lines |
-c, -n | Count matches or show line numbers |
-q | No output; exit status only |
More search patterns live on the grep sheet.
grep -E '^[[:space:]]*#' app.conf
grep -Ev '^[[:space:]]*(#|$)' app.conf
grep -Eo '[0-9]{1,3}(\.[0-9]{1,3}){3}' access.log
grep -Ei 'warn(ing)?|error' build.log
Quote patterns in single quotes so the shell leaves $, * and | alone.
ERE (grep -E) | BRE (grep) |
|---|---|
a+, a? | aa*, a\{0,1\} |
(ab) | \(ab\) |
a{2,3} | a\{2,3\} |
a|b | no portable equivalent |
Back-references such as \1 belong to BRE; strict ERE has none.
| Perl-style (PCRE, JavaScript) | POSIX ERE spelling |
|---|---|
\d, \D | [[:digit:]], [^[:digit:]] |
\s, \S | [[:space:]], [^[:space:]] |
\w, \W | [[:alnum:]_], [^[:alnum:]_] |
\b | no ERE equivalent; try grep -w |
macOS accepts these shortcuts only in its "enhanced" mode, and spells word edges [[:<:]] and [[:>:]].
(?:abc) group without capturing
a+? a*? lazy (shortest) repetition
(?=next) lookahead
(?<year>\d{4}) named group
(?i)abc inline case-insensitive flag
These belong to backtracking engines such as PCRE, JavaScript or the Oniguruma engine used by jq. Test such patterns in the tool that will run them.