AI Hints
Home›NLP Tutorial›Cleaning and Splitting Text
NLP · Lesson 4 of 106

Regular Expressions

Finding patterns in text.

⏱ 2 min read▶ Video at 7:24🎓 Free
Watch this lesson in the NLP courseWatch on YouTube ↗Full NLP course

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.

Regular Expressions
From the video: Regular Expressions
  • 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.

extract.pypython
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))
Output
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.

cleaning.pypython
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"))
Output
['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
Next up: Tokenisation — splitting text into pieces