Regular expressions are one of the most powerful tools in a developer's toolkit — and one of the most avoided. This practical cheatsheet covers everything from basic character matching to lookaheads, with real-world examples in Python, JavaScript, and grep.
RegexPythonJavaScriptgrepPattern Matching
Character Classes
Pattern
Matches
.
Any character except newline
\d
Any digit (0-9)
\D
Any non-digit
\w
Word character (a-z, A-Z, 0-9, _)
\W
Non-word character
\s
Whitespace (space, tab, newline)
\S
Non-whitespace
[aeiou]
Any character in the set
[^aeiou]
Any character NOT in the set
[a-z]
Any lowercase letter
[A-Za-z0-9]
Alphanumeric character
Quantifiers
Pattern
Meaning
*
Zero or more (greedy)
+
One or more (greedy)
?
Zero or one (optional)
{3}
Exactly 3 times
{2,5}
Between 2 and 5 times
{3,}
3 or more times
*?
Zero or more (lazy / non-greedy)
+?
One or more (lazy)
Greedy quantifiers match as much as possible. Lazy quantifiers (add ?) match as little as possible. This matters when your pattern is inside a larger string with repeating segments.
# Python
import re
clean = re.sub(r'\s+', ' ', ' hello world ').strip()
print(clean) # 'hello world'
Extract Named Groups (Python)
import re
log_line = '2024-05-29 14:32:11 ERROR Failed to connect to database'
pattern = r'(?P\d{4}-\d{2}-\d{2}) (?P
grep with Extended Regex
# Match lines with an IP address
grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' access.log
# Match lines containing ERROR or WARN
grep -E 'ERROR|WARN' app.log
# Extract just the matched part
grep -oE '"[A-Z]+ /[^"]+"' access.log
Flags / Modifiers
Python flag
JS flag
Effect
re.IGNORECASE (re.I)
i
Case-insensitive matching
re.MULTILINE (re.M)
m
^ and $ match line starts/ends
re.DOTALL (re.S)
s
. matches newlines too
re.VERBOSE (re.X)
(none)
Allow whitespace and comments in pattern
(none)
g
Global: find all matches (not just first)
Tips for Writing Readable Regex
Long regex patterns become unmaintainable. Use Python's re.VERBOSE mode to add comments and whitespace:
import re
pattern = re.compile(r'''
^ # Start of string
(\d{4}) # Year (4 digits)
-
(0[1-9]|1[0-2]) # Month (01-12)
-
(0[1-9]|[12]\d|3[01]) # Day (01-31)
$ # End of string
''', re.VERBOSE)
print(bool(pattern.match('2024-05-29'))) # True