| Pattern | Description |
|---|---|
| ^ | Start of line |
| $ | End of line |
| \b | Word boundary |
| \B | Non-word boundary |
| Pattern | Description |
|---|---|
| . | Any single character (except newline) |
| [abc] | One of a, b, or c |
| [^abc] | Any character except a, b, or c |
| [a-z] | Any lowercase letter |
| [A-Z] | Any uppercase letter |
| [0-9] | Any digit |
| [a-zA-Z0-9] | Any alphanumeric character |
| \d | Any digit (same as [0-9]) |
| \D | Any non-digit |
| \w | Any word character (letter, digit, underscore) |
| \W | Any non-word character |
| \s | Any whitespace (space, tab, newline) |
| \S | Any non-whitespace character |
\d, \w, and \s are Perl-style shortcuts. They work in `grep -P` and most programming languages but not in basic POSIX regex.
| Class | Description |
|---|---|
| [:alpha:] | Alphabetic characters |
| [:digit:] | Digits (0-9) |
| [:alnum:] | Alphanumeric characters |
| [:space:] | Whitespace characters |
| [:upper:] | Uppercase letters |
| [:lower:] | Lowercase letters |
| [:punct:] | Punctuation characters |
| [:print:] | Printable characters |
| [:blank:] | Space and tab |
| Pattern | Description |
|---|---|
| ***** | Zero or more times |
| + | One or more times |
| ? | Zero or one time |
| {n} | Exactly n times |
| {n,} | n or more times |
| {n,m} | Between n and m times |
In basic regex (BRE), quantifiers `+`, `?`, `{`, and `}` must be escaped with a backslash. Use `grep -E` for extended regex where they work without escaping.
| Pattern | Description |
|---|---|
| (abc) | Group — match "abc" as a unit |
| a|b | Alternation — match a or b |
| \1 | Backreference — match the first captured group again |
| \2 | Backreference — match the second captured group |
Curated for the Linux community