-
Import the module:
import re -
Basic match:
re.search(r'cat', 'concatenate')→ finds'cat'anywhere. -
Full match:
re.fullmatch(r'cat', 'cat')→ must match the entire string. -
Find all:
re.findall(r'\d+', 'a1b22c333')→['1', '22', '333'] -
Replace:
re.sub(r'\s+', ' ', 'too many spaces')→'too many spaces' -
Character classes:
\ddigits,\wword chars,\swhitespace.any char except newline
-
Quantifiers:
+one or more,*zero or more,?optional,{n,m}range
-
Anchors:
^start,$end,\bword boundary -
Groups:
re.search(r'(ha)+', 'hahaha').group(1)→'ha'- Named:
(?P<name>pattern)
-
Raw strings: Always use
r'pattern'to avoid problems with escaping.
Created
October 11, 2025 18:39
-
-
Save dbohdan/7de18b7a564978d073283dfae5b0213f to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment