Python
text patterns
machine learning
natural language processing
data analysis

Python - A way to learn and detect text patterns?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In Python, the right way to detect text patterns depends on the kind of pattern you mean. If the rule is explicit, such as an email address or invoice number, regular expressions are usually the best tool. If the pattern must be learned from examples, use a text-processing pipeline and a model.

Start with Rule-Based Matching

For known text shapes, Python's re module is the fastest path. It lets you define patterns such as dates, order IDs, hashtags, or repeated word structures.

A simple example extracts invoice codes like INV-2026-1042:

python
1import re
2
3text = """
4Paid invoices: INV-2026-1042, INV-2026-1043.
5Ignore draft id TEMP-77.
6"""
7
8pattern = re.compile(r"\bINV-\d{4}-\d{4}\b")
9matches = pattern.findall(text)
10print(matches)

Output:

text
['INV-2026-1042', 'INV-2026-1043']

This is ideal when the signal is structural. You know what the text should look like, and the program only needs to recognize it.

Use raw strings such as r"\d+" for regex patterns. That keeps backslashes readable and avoids accidental escaping mistakes.

Normalize Before Matching

Pattern detection gets more reliable when you clean the text first. Even a light preprocessing step can remove casing and punctuation differences that would otherwise hide the pattern.

python
1import re
2
3
4def normalize(text: str) -> str:
5    text = text.lower()
6    text = re.sub(r"[^a-z0-9\s]", " ", text)
7    text = re.sub(r"\s+", " ", text).strip()
8    return text
9
10sample = "Error: Disk FULL!!!"
11print(normalize(sample))

Output:

text
error disk full

This matters when your rules are concept-level rather than punctuation-level. For example, Disk full, disk FULL, and disk-full should often map to the same underlying message.

Learn Patterns from Labeled Examples

Regex works poorly when the wording varies too much. Suppose you want to classify support messages into categories such as billing, login, or shipping. In that case, you usually want a model that learns which words and phrases correlate with each label.

A small scikit-learn pipeline is enough for many practical tasks:

python
1from sklearn.feature_extraction.text import CountVectorizer
2from sklearn.linear_model import LogisticRegression
3from sklearn.pipeline import make_pipeline
4
5train_texts = [
6    "reset my password",
7    "cannot sign in to my account",
8    "invoice amount is incorrect",
9    "billing page charged me twice",
10    "package has not arrived",
11    "tracking number shows delayed shipment",
12]
13
14train_labels = [
15    "login",
16    "login",
17    "billing",
18    "billing",
19    "shipping",
20    "shipping",
21]
22
23model = make_pipeline(
24    CountVectorizer(),
25    LogisticRegression(max_iter=1000)
26)
27
28model.fit(train_texts, train_labels)
29
30predictions = model.predict([
31    "I was charged twice",
32    "I cannot log in",
33    "Where is my package",
34])
35
36print(predictions)

This kind of pipeline learns a statistical pattern from examples instead of relying on a hand-written rule. It is often the right answer when the same intent appears in many different phrasings.

Choosing Between Rules and Learning

A practical decision rule is simple:

  • Use regex when the pattern has a stable shape.
  • Use a learned model when wording varies but labels are known.
  • Use both when structure and intent matter together.

For example, you might first extract product codes with regex and then feed the cleaned message into a classifier that predicts the request type.

You can combine both approaches cleanly:

python
1import re
2
3
4def extract_order_id(text: str) -> str | None:
5    match = re.search(r"\bORD-\d{6}\b", text)
6    return match.group(0) if match else None
7
8message = "Please refund order ORD-123456 because the item arrived damaged."
9print(extract_order_id(message))

After extracting the order ID, a separate model can classify the request as refund, replacement, or delivery issue.

Common Pitfalls

One common mistake is using regex for problems that are not actually pattern-shaped. If users can write the same idea in dozens of ways, the regular expression quickly becomes brittle and unreadable.

Another issue is training a model on too little or too noisy data. A classifier can only learn the patterns present in the examples you provide. If labels are inconsistent, the model will be inconsistent too.

A third problem is skipping preprocessing. Small differences in casing, punctuation, and whitespace often reduce match quality for both regex and learned models.

Finally, do not confuse detection with understanding. A pattern detector can find text that looks like an invoice number or predict that a sentence is about billing. That does not mean it fully understands the document. Keep the scope narrow and measurable.

Summary

  • Use Python regex for explicit text shapes such as IDs, dates, and codes
  • Normalize text before matching to improve consistency
  • Use scikit-learn or another model when the pattern must be learned from examples
  • Combine rule-based extraction with classification when both structure and intent matter
  • Pick the simplest method that matches the problem instead of forcing machine learning everywhere

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.