Regex
A pattern language for searching, validating and replacing text by a template. Instead of an exact string you define a rule: find all emails, or extract all links.
Example
import re
re.findall(r"https?://[^\s]+", text) # all URLs in the text
re.sub(r"\s+", " ", text) # collapse the spaces
Where It Is Used
- parsing HTML and logs;
- validating formats (email, phone);
- extracting data from text;
- search and replace in files.
A Nuance
Regexes are powerful but hard to read. A complex pattern is better split into parts or replaced with a parser.