Every other regex feature builds on knowing which characters are literal and which are special.
A regular expression (regex) is a small pattern that describes a set of strings. An engine scans the text and reports where the pattern matches. Most characters are literals that match themselves: the pattern cat finds cat in concatenate.
A few characters are metacharacters with special meaning:
. ^ $ * + ? ( ) [ ] { } | \To match one of them literally, escape it with a backslash: 3\.14 matches 3.14 but not 3x14; \( matches a parenthesis. Inside a character class [...] most of them lose their special meaning, so [.] is another way to match a literal dot.
Two ideas to keep in mind from the start:
- Matching is usually a search: the pattern may match anywhere in the string unless you anchor it. Many APIs have both a "search" and a "match the whole string" function (in Python
re.searchversusre.fullmatch). - Regexes live inside string literals, which have their own escaping. Use raw strings in Python (
r"\d+") and regex literals in JavaScript (/\d+/) so you don't have to double every backslash.
When you need to match arbitrary user text literally, don't escape it by hand: use re.escape() in Python or the equivalent in your language.
Example
import re
print(re.search(r"cat", "concatenate")) # <re.Match object; span=(3, 6), match='cat'>
print(re.findall(r"3.14", "3.14 3x14")) # ['3.14', '3x14'] . matches anything
print(re.findall(r"3\.14", "3.14 3x14")) # ['3.14']
print(re.fullmatch(r"cat", "concatenate")) # None: the whole string must match
user_input = "1+1=2?"
pattern = re.escape(user_input)
print(pattern) # 1\+1=2\?
print(bool(re.search(pattern, "is 1+1=2?"))) # TrueRemember
- Most characters are literal; . ^ $ * + ? ( ) [ ] { } | \ are special
- Escape metacharacters with \ or put them in [ ]
- Use raw strings (r"...") and re.escape for literal user input
Watch out for
- Searching for a version like 1.2.3 with an unescaped pattern, which also matches 1x2y3.