Regular Expressions
Finding patterns in text.
Before any neural network touches it, somebody has to find the order number in, hi, my order hash four eight two one three never came. The tool for that was invented in the nineteen fifties, and it's in every programming language today. Regular expressions are a tiny language for describing patterns.

- Find the order number
- Invented in the 1950s
- A tiny language for patterns
Pulling out facts
Scribe's first skill. Here's a real looking ticket. A regular expression describes the shape of what you want. Backslash d means any digit, and plus means one or more. Hash, then digits, finds the order number. The second pattern matches word characters, then an at sign, then more. And the third has exactly three digits, a dash, and four more. Run it. Three facts, pulled out of messy text instantly, with no AI at all, and at practically no cost. For fixed patterns like these, regex beats any model.
import re
ticket = "Hi, order #48213 hasn't come. Reach me on sam@mail.com or 555-0192."
print("Order:", re.findall(r"#(\d+)", ticket))
print("Email:", re.findall(r"[\w.]+@[\w.]+", ticket))
print("Phone:", re.findall(r"\d{3}-\d{4}", ticket))Order: ['48213'] Email: ['sam@mail.com'] Phone: ['555-0192']
Cleaning with patterns
Regular expressions also clean text. The first line finds reference numbers. Add the ignore case flag, and it finds the lower case one too. Sub replaces a pattern, so here it hides an email address, which is a simple way to protect privacy before text goes anywhere else. And backslash s plus squashes runs of spaces into one. Run it. Messy text, tidied in a single line.
import re
text = "Refund REF-2291 and REF-77 please. Not ref-12, that one is old."
print(re.findall(r"REF-\d+", text))
print(re.findall(r"REF-\d+", text, flags=re.IGNORECASE))
print(re.sub(r"\b[\w.]+@[\w.]+\b", "[EMAIL]", "Mail sam@mail.com now"))
print(re.sub(r"\s+", " ", "too many spaces"))['REF-2291', 'REF-77'] ['REF-2291', 'REF-77', 'ref-12'] Mail [EMAIL] now too many spaces
Summary
Regular expressions find fixed patterns, cheaply and exactly. They're perfect for order numbers, emails, and dates. But they can't understand meaning. For that, we first need to break text into pieces. And that's harder than it looks.
Key takeaways- \d digits, \w word characters
- Use for fixed patterns
- Hide private data with sub