Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 63 ยท Language Models Before Neural Nets

Rule-Based Information Extraction

Extract emails, phone numbers, dates and prices from text with Python regular expressions, and learn the pitfalls: greediness, boundaries and validation.

Not every language task needs a model. If the thing you want has a predictable shape, such as an email address, a date, a price, an order number or a phone number, a handful of regular expressions will find it faster, cheaper and more transparently than any trained system. This is rule-based information extraction: you write down the pattern, and the program applies it to every document.

Rules have a bad reputation because they break on the long tail of real text. That is a fair warning, not a reason to avoid them. Rules are the right first tool for well-structured items, the baseline you must beat before using machine learning, and a common building block inside bigger pipelines (many cleaning steps use the same patterns). This lesson shows the patterns that matter and the traps that waste afternoons. It assumes the regex basics from Working with Text in Python.

Strategy: match loosely, then validate

A reliable extractor usually has two stages:

  1. Find candidates with a regular expression that is permissive enough to catch real cases.
  2. Validate or normalize each candidate in ordinary code: check that a month is between 1 and 12, that a date exists on the calendar, or turn "1,250.00" into the number 1250.0.

Regular expressions describe shapes, not meaning. It is possible to write a regex for "a valid date" that rejects the 13th month, but it is much clearer to match \d{4}-\d{2}-\d{2} and let datetime decide. Keep the regex readable, and let code do the checking.

A rule-based extractor

Pattern Describe the shape with a regex.
Find Use finditer to get every match and its position.
Validate Check meaning in plain code.
Normalize Store one clean format.

Loose pattern first, strict checks second.

1 / 2

Emails and phone numbers

An email address is a local part, an @, and a domain with at least one dot. A practical pattern is [A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+. It deliberately does not follow the full email standard, which allows strange things almost nobody uses; for extraction, "good enough and conservative" is the right target.

Phone numbers vary wildly by country. The pattern below handles one North American style (three digits, three digits, four digits, with optional parentheses and separators), captures the three parts as groups, and joins them to normalize the number.

Python Emails and phone numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The careful pattern returns the three real addresses and ignores user@, @handle and the typo. The loose \S+@\S+ pattern shows why shortcuts hurt: it drags along trailing commas and periods ('[email protected],') and matches the garbage user@, and bob@@example.com.. Notice the last email is [email protected] followed by a sentence period; the careful pattern correctly stops before the final dot because a domain label has to continue with letters. For phone numbers, finditer gives full matches, the groups, and the position (span), which you need if you want to highlight or remove the match later.

Dates

Dates are a good example of why you want several small patterns instead of one giant one. Text contains ISO dates (2026-03-14), slashed dates (31/12/2026) and written ones (March 5, 2026, 5 Apr 2026). Named groups ((?P<y>...)) make each pattern self-documenting and give you a dictionary per match, so the same code can convert any format to a standard one later.

Slash dates are ambiguous: 03/04/2026 is March 4 in the United States and April 3 in most of the world. No regex can resolve that. You must know the source's convention, or flag the date as ambiguous.

Prices

Money has symbols, separators and decimal marks. A currency symbol before the number ($4.99), a code or sign after it (15 EUR, 7,5 โ‚ฌ), thousands separators (1,250.00) and decimal commas all appear in real text. Keep the number and the currency in separate capture groups when you need both.

Python Dates and prices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each date format is found once, with its parts in a dictionary. The ISO pattern restricts the month to 01 to 12 and the day to 01 to 31, so 2026-13-45 yields an empty list. Even so, that pattern would accept 2026-02-30, which does not exist: regexes check shape, not the calendar. The price pattern finds $4.99, $1,250.00, 15 EUR, 7,5 โ‚ฌ and $ 3, and skips the bare year 2026 because a price needs a symbol or currency code.

Pitfalls that bite

Most regex failures fall into a small number of categories. The next example shows several of them with tiny inputs.

Python Five classic regex pitfalls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the output as a checklist:

  • Greedy matching. .* swallows as much as it can, so the greedy pattern returns one giant match covering both tags. Adding ? makes it lazy and returns two. For HTML, though, use a real parser: regexes cannot handle nested tags reliably.
  • Missing word boundaries. Without \b, cat matches inside concatenate. Boundaries make "whole word" explicit.
  • Numbers are not one thing. \d+ splits 1,250 into 1 and 250 and 3.14 into 3 and 14. Decide what a number looks like in your text and write that pattern.
  • match versus search. re.match only tries the start of the string, which is why it returned None for text with a prefix. Use search or finditer to scan.
  • Shape is not validity. The pattern accepted a format; datetime is what proves February 30 does not exist.

Two more pitfalls are worth knowing, even though they are not run here. Case and Unicode: [A-Za-z] does not match accented letters, so names like Zoรซ are missed unless you use \w (Unicode-aware in Python 3) or re.IGNORECASE. And catastrophic backtracking: nested repetitions such as (a+)+$ can take exponential time on an input that almost matches, which makes a regex into a denial-of-service hole when it runs on untrusted text. Prefer simple patterns, avoid nested quantifiers, and put a length limit on inputs.

Do not try to parse HTML, nested brackets or full programming-language syntax with regular expressions. When the structure can nest, use a real parser. Regexes are for flat, local patterns.

When to stop writing rules

Rules stay precise but lose recall as the text gets messier. A useful test: write your patterns, run them on a hundred real documents, and read the misses. If the misses follow a few patterns, add rules. If every miss is different (names of people, companies, products, anything defined by meaning instead of shape), you have hit the point where a learned model such as named entity recognition is the better tool. Many production systems combine both: rules for the rigid items, a model for the rest, and a rule layer afterwards to validate what the model returned.

Project idea: a resume parser

A resume parser turns a semi-structured document into fields: name, contact data, education, experience and skills. Libraries such as pyresparser wrap spaCy and NLTK for this and need downloaded models, but the core is the same techniques as the rest of this lesson. Regular expressions find the emails, phone numbers and years; a dictionary of known skills is matched against the lowercased text; and section headings (Education, Experience, Skills) split the document into blocks. For the name and organizations a named entity recognizer does better than rules, and the education or experience blocks can be classified by their heading.

Python A rule-based resume parser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The parser reads the first line as the name, finds the email with one pattern and the phone number with another, and matches each skill as a whole word (\b...\b), so Pandas, Git and Machine Learning are all found. The section splitter then groups lines under each heading, and the span of years in the experience block gives a rough length of experience. The limits are clear: a skill missing from the dictionary is invisible, resumes with unusual headings or two-column layouts break the section logic, and text extracted from PDF files may come out in the wrong order (see OCR and Text from Documents). That is why production parsers combine rules with trained models.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is it often better to match a date loosely with a regex and then validate it in code?

    A regex can check that a month is 01 to 12, but it cannot easily know which days exist in which month and year. Matching the shape first and validating with the datetime module keeps both parts simple.
  2. 2. What does the lazy quantifier in <b>.*?</b> change compared with the greedy <b>.*</b>?

    A greedy .* expands as far as possible and swallows everything up to the last closing tag. Adding ? makes it stop at the first closing tag it finds.
  3. 3. Why does re.match return None for the string "x 2026-03" and a pattern that looks for four digits, a hyphen and two digits?

    re.match anchors at the beginning, and the string starts with "x". re.search scans the whole string and finds the date.
  4. 4. Which task is the weakest fit for a rule-based approach?

    Company names have no fixed shape; they are defined by meaning and context. That is where a learned entity recognizer helps, while the other items are rigid formats that rules handle well.

Summary

  • Rule-based extraction uses regular expressions for items with a predictable shape: emails, phone numbers, dates, prices and identifiers.
  • Match loosely, then validate and normalize in code; use finditer, groups and named groups to get positions and parts.
  • Dates need one small pattern per format, and some formats (such as 03/04/2026) are ambiguous without outside knowledge.
  • Watch for greedy quantifiers, missing \b boundaries, match versus search, Unicode letters and catastrophic backtracking.
  • Use rules for rigid formats and as a baseline; switch to learned models when the target is defined by meaning, not shape.

Next, start classifying whole documents with the Text Classification Overview.