RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Python for AI — Zero to Useful
  6. /Ch. 22
Python for AI — Zero to Useful

22. Regular Expressions

Chapter 22 of 36 · 15 min
KEY INSIGHT

Regex patterns use metacharacters: `.` (any char), `\d` (digit), `\w` (word char), `+` (one or more), `*` (zero or more), `[]` (character class). Escape with `\` for literal versions: `\.` for dot, `\+` for plus.

Regular expressions (regex) are text extraction and transformation tools. In AI work, you'll use them to parse logs, extract structured data from messy text, and validate inputs before they hit your models.

Python's re module is your interface:

import re

# Match pattern anywhere in string
text = "Result: accuracy=0.9234, loss=0.123"
accuracy = re.search(r'accuracy=([0-9.]+)', text)
print(accuracy.group(1))  # '0.9234'

# Find all matches
logs = """
2024-01-15 ERROR: Failed to process doc_123.pdf
2024-01-16 INFO: Success for batch 42
2024-01-17 ERROR: Timeout on api-v2.doc
"""
errors = re.findall(r'ERROR: (.+)', logs)
print(errors)  # ['Failed to process doc_123.pdf', 'Timeout on api-v2.doc']

# Replace
cleaned = re.sub(r'(\w+)_(\d+)\.pdf', r'\1-\2.txt', "doc_123.pdf")
print(cleaned)  # 'doc-123.txt'

The r'' raw string notation is critical for regex patterns—it prevents escape sequence interpretation. Capture groups (parentheses) let you extract specific parts; \1, \2 in replacements refer to them.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Given this model log output:

MODEL Training Results
Epoch 1: loss=2.345, acc=0.123
Epoch 2: loss=1.876, acc=0.456
Epoch 3: loss=1.234, acc=0.678

Write a Python script using regex to extract all (epoch, loss, accuracy) tuples. Print them as a list of dicts.

← Chapter 21
Inheritance and Composition
Chapter 23 →
Text Processing with Regex