Regular Expression
Regular Expression Cheat Sheet
Regular Expression Websites
https://regexone.com/ - Very nice and quick questions to learn more about regex
Basic Matching
Each symbol matches a single character
Character Classes
Escape Sequences
"Escaping" is a way of treating characters which have a special meaning in regular expressions literally, rather than as special characters. The escape character is usually \
Boundaries
Boundary characters are helpful in "anchoring" your pattern to some edge, but do not select any characters themselves
Example: \bcat\b
finds a match in "the cat in the hat" but not in "locate"
Quantifiers
By default quantifiers just apply to the one character. Use (...) to specify explicit quantifier "scope"
Disjunction
Example: \b(cat|dog)s\b
matches cats and dogs.
Special Characters
The character {} [] ^ $ . | * + > \
(and - inside [...]) have special meaning in regex, so they must be "escaped" with \ to match them
Example: \.
matches the period .
and \\
matches the backslash \
Backreferences
Count your open parentheses (
from the left, starting with 1. Whatever is matched by parthesis number n
can be refernced later by \n
Example: \b(\w+) \1\b
matches two identical words with a space in between
Example 2: \b(\w+)er\b
and replacing with more \1
will map "the taller man" -> "the more tall man" and "I am shorter" -> "I am more short"
Last updated