Working with unstructured text.
WHAT PYTHON DOES WELL
Searching, splitting, replacing and extracting from text.
WHAT STRING METHODS COVER
Most everyday needs.
Use them before reaching for anything more.
WHAT REGULAR EXPRESSIONS ARE
A notation for describing patterns in text.
WHEN THEY SUIT
Extracting values in a known format Validating a format Complex search and replace
WHEN THEY DO NOT
Parsing structured formats such as HTML or CSV.
Use a proper parser for those.
THAT POINT
Attempting to parse markup with patterns produces fragile code that fails on valid input.
WHAT TO BE CAREFUL WITH
Patterns that are hard to read Patterns that can take a very long time on certain input Assuming a pattern matches only what you intended
WHAT TO DO
Comment any non-trivial pattern Test it against awkward examples, including ones that should not match
WHAT TO PREFER
A simple pattern plus a check, over one elaborate pattern.
WHAT TO VALIDATE
That extraction produced what you expected, not merely something.