Lesson 20 of 20

Project Structure

Regular Expressions

2 min read

Regular expressions (regex) are patterns for matching text. They're useful for parsing AI responses, extracting data, and validating input.

Basic Pattern Matching

import re

text = "Contact us at support@example.com"

# Search for pattern
match = re.search(r"@\w+\.\w+", text)
if match:
    print(match.group())  # "@example.com"

# Find all matches
emails = re.findall(r"\S+@\S+", text)

Common Patterns

Pattern Matches Example
\d Digit 0-9
\w Word character a-z, A-Z, 0-9, _
\s Whitespace space, tab, newline
. Any character anything except newline
+ One or more \d+ matches "123"
* Zero or more \d* matches "" or "123"
? Zero or one \d? matches "" or "5"
[] Character set [aeiou] matches vowels

Extracting Data from AI Responses

import re

# Extract code blocks from markdown
response = """
Here's the code:
```python
def hello():
    print("Hello!")

"""

code_pattern = r"(\w+)?\n(.*?)" matches = re.findall(code_pattern, response, re.DOTALL) for lang, code in matches: print(f"Language: {lang}") print(f"Code: {code}")


## Search, Match, and Findall

```python
import re

text = "Order #12345 shipped on 2024-01-15"

# search: Find first match anywhere
result = re.search(r"#(\d+)", text)
print(result.group(1))  # "12345"

# match: Match at start only
result = re.match(r"Order", text)  # Works
result = re.match(r"shipped", text)  # None (not at start)

# findall: Find all matches
numbers = re.findall(r"\d+", text)  # ["12345", "2024", "01", "15"]

Substitution

import re

# Replace patterns
text = "My phone is 123-456-7890"
masked = re.sub(r"\d", "X", text)
# "My phone is XXX-XXX-XXXX"

# Replace with captured groups
text = "Date: 2024-01-15"
formatted = re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\2/\3/\1", text)
# "Date: 01/15/2024"

Practical AI Examples

import re

# Extract tool calls from agent output
output = "I'll use search(query='Python tutorials') to find that."
tool_pattern = r"(\w+)\(([^)]+)\)"
match = re.search(tool_pattern, output)
if match:
    tool_name = match.group(1)  # "search"
    args = match.group(2)       # "query='Python tutorials'"

# Parse structured output
response = "ANSWER: The capital of France is Paris."
answer_pattern = r"ANSWER:\s*(.+)"
match = re.search(answer_pattern, response)
if match:
    answer = match.group(1)  # "The capital of France is Paris."

# Validate API key format
def is_valid_openai_key(key):
    pattern = r"^sk-[A-Za-z0-9]{48}$"
    return bool(re.match(pattern, key))

Quick Reference

import re

# Common operations
re.search(pattern, text)     # Find first match
re.match(pattern, text)      # Match at start
re.findall(pattern, text)    # Find all matches
re.sub(pattern, repl, text)  # Replace matches
re.split(pattern, text)      # Split by pattern

# Flags
re.IGNORECASE  # Case-insensitive
re.DOTALL      # . matches newlines
re.MULTILINE   # ^ and $ match line boundaries

Congratulations! You've completed the Python for AI Development course. You're now ready to build AI agents! :::

Quiz

Module 5: Project Structure

Take Quiz