Linux - Common Regex Expressions for grep Cheat Sheet
This introductory guide serves as a handy reference for common regular expressions (regex) you can use with the grep command. If you’re just starting out or need a quick refresh, this cheat sheet will make it easy to get the hang of the essential regex patterns that go hand in hand with grep.
Basic Patterns
.: Matches any single character- Example:
grep "h.t" file.txtmatches “hat”, “hot”, “hit”, etc.
- Example:
^: Matches the start of a line- Example:
grep "^The" file.txtmatches lines starting with “The”
- Example:
$: Matches the end of a line- Example:
grep "end$" file.txtmatches lines ending with “end”
- Example:
*: Matches zero or more occurrences of the previous character- Example:
grep "ca*t" file.txtmatches “ct”, “cat”, “caat”, etc.
- Example:
Character Classes
[abc]: Matches any single character in the set- Example:
grep "[aeiou]" file.txtmatches any vowel
- Example:
[^abc]: Matches any single character not in the set- Example:
grep "[^0-9]" file.txtmatches any non-digit
- Example:
[a-z]: Matches any single character in the range- Example:
grep "[A-Z]" file.txtmatches any uppercase letter
- Example:
[a-zA-Z]: Matches any lowercase or uppercase letter- Example:
grep "[a-zA-Z]" file.txtmatches any letter
- Example:
Note: Character classes are case-sensitive unless used with the -i flag.
Basic vs Extended Regular Expressions
- Use
grep -Efor extended features such as+,?, and|. - For basic usage, use
grepwithout the-Eflag, though it has more limited regex capabilities.
Extended Regex Features (use with grep -E)
+: Matches one or more occurrences of the previous character- Example:
grep -E "ca+t" file.txtmatches “cat”, “caat”, but not “ct”
- Example:
?: Matches zero or one occurrence of the previous character- Example:
grep -E "colou?r" file.txtmatches both “color” and “colour”
- Example:
|: Alternation (OR)- Example:
grep -E "cat|dog" file.txtmatches lines containing either “cat” or “dog”
- Example:
Quantifiers
{n}: Matches exactly n occurrences- Example:
grep -E "a{3}" file.txtmatches “aaa”
- Example:
{n,}: Matches n or more occurrences- Example:
grep -E "a{2,}" file.txtmatches “aa”, “aaa”, etc.
- Example:
{n,m}: Matches between n and m occurrences- Example:
grep -E "a{2,4}" file.txtmatches “aa”, “aaa”, “aaaa”
- Example:
Special Characters
\: Escapes special characters- Example:
grep "\." file.txtmatches actual periods
- Example:
\b: Matches word boundaries- Example:
grep "\bthe\b" file.txtmatches “the” but not “there” or “other”
- Example:
Practical Examples
- Find all IP addresses:
grep -E "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" file.txt - Match email addresses:
grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}" file.txt - Find lines with dates (YYYY-MM-DD format):
grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}" file.txt - Match lines starting with “Error” or “Warning”:
grep -E "^(Error|Warning)" log.txt - Find words with at least eight characters:
grep -E "\b[A-Za-z]{8,}\b" file.txt - Match lines that do not contain “Error” (inverse match):
grep -v "Error" log.txt - Case-insensitive search for the word “hello”:
grep -i "hello" file.txt
Backslash Characters
When using grep -E or grep -P (PCRE mode), the following backslash characters have special meanings:
\b: Word boundary- Example:
grep -E "\bcat\b" file.txtmatches “cat” but not “category”
- Example:
\B: Non-word boundary- Example:
grep -E "\Bcat\B" file.txtmatches “cat” in “concatenate” but not “cat” alone
- Example:
\d: Any digit (equivalent to [0-9])- Example:
grep -E "\d{3}" file.txtmatches any three consecutive digits
- Example:
\D: Any non-digit (equivalent to [^0-9])- Example:
grep -E "\D+" file.txtmatches one or more non-digit characters
- Example:
\s: Any whitespace character (space, tab, newline)- Example:
grep -E "hello\sworld" file.txtmatches “hello world” with any whitespace between
- Example:
\S: Any non-whitespace character- Example:
grep -E "\S{5}" file.txtmatches any five consecutive non-whitespace characters
- Example:
\w: Any word character (alphanumeric + underscore, equivalent to [a-zA-Z0-9_])- Example:
grep -E "\w+" file.txtmatches one or more word characters
- Example:
\W: Any non-word character- Example:
grep -E "\W" file.txtmatches any single non-word character
- Example:
\\: Literal backslash- Example:
grep -E "\\" file.txtmatches a single backslash character
- Example:
ℹ️ Note: When using basic
grep(without -E or -P), you may need to escape these special backslash characters with an additional backslash, like\\sor\\d.
Practical Examples with Backslash Characters
- Find words starting with “pre”:
grep -E "\bpre\w+" file.txt - Match phone numbers in format (xxx) xxx-xxxx:
grep -E "\(\d{3}\)\s?\d{3}-\d{4}" file.txt - Find lines with exactly 3 words:
grep -E "^\s*\S+\s+\S+\s+\S+\s*$" file.txt - Match email addresses using \w:
grep -E "\b[\w.%+-]+@[\w.-]+\.[A-Za-z]{2,}\b" file.txt