NOTE

Also valid for: grep -P (add -P flag for PCRE mode — covers almost everything below)

TIP

Once you have matched what you want with a regex, to select all occurrences found: ALT + ENTER


Shorthand Character Classes

PatternMatches
\dAny digit (0–9)
\wAny word character (letters and digits)
\sAny whitespace (including \n and \t)
\nNewline
\tTab

Uppercase = opposite: \D matches anything that is NOT a digit, \W not a word character, \S not whitespace.


Anchors

PatternMeaning
^Start of line (outside a character class)
$End of line
[^...]Negation inside a class — excludes everything listed. Example: [^0-4] excludes digits 0–4

Wildcards & Quantifiers

PatternMeaning
.Any single character
?0 or 1 of the preceding element — makes it optional. Example: .docx? matches .doc and .docx
+1 or more of the preceding element. Example: [0-9]+ matches 8 or 3451234567
*0 or more of the preceding element. Example: 10* matches 1, 10, 100, 1000
{n,m}Between n and m occurrences. Example: \w{2,3} matches ww, www, 123

Quantifier equivalences:

? = {0,1}
+ = {1,}
* = {0,}

Lazy qualifiers: out of scope for now.


Boundaries

\b specifies a word boundary — useful to match exact words:

PatternMatches
\bcalmWords that start with calm
calm\bWords that end with calm
\bcalm\bExactly the word calm and nothing else

Groups & Alternation

Groups are defined with parentheses (). Operators and quantifiers can be applied to the whole group. Alternation uses | (OR) inside a group.

Example 1 — generic:

(\w+)([ -])?(Man)

Matches: Spider-Man, Super Man, Batman

Example 1 — more specific:

(spider|super|bat)[- ]?[Mm]an

Example 2:

(High|Mid|Low)-level

Matches: High-level, Mid-level, Low-level


Exercises

Match a phone number

[0-9]{3}-[0-9]{2}-[0-9]{4}

Match an IPv4 address

\b\d{1,3}(\.\d{1,3}){3}\b