# How Regular Expressions Are Introduced in the 30 Days of Python Curriculum

> Discover how regular expressions are taught in 30 Days of Python. Learn re module API, pattern syntax, and practice with hands-on exercises on Day 18.

- Repository: [Asabeneh/30-Days-Of-Python](https://github.com/Asabeneh/30-Days-Of-Python)
- Tags: deep-dive
- Published: 2026-03-06

---

**The 30 Days of Python curriculum introduces regular expressions on Day 18 through a structured lesson in [`18_Day_Regular_expressions/18_regular_expressions.md`](https://github.com/Asabeneh/30-Days-Of-Python/blob/main/18_Day_Regular_expressions/18_regular_expressions.md) that covers the `re` module API, pattern syntax, and progressive hands-on exercises.**

The Asabeneh/30-Days-Of-Python repository dedicates Day 18 to regular expressions, providing a self-contained module that teaches text pattern matching from first principles. Located at [`18_Day_Regular_expressions/18_regular_expressions.md`](https://github.com/Asabeneh/30-Days-Of-Python/blob/main/18_Day_Regular_expressions/18_regular_expressions.md), this lesson combines conceptual explanations with runnable code snippets to bridge theory and practice.

## How Day 18 Introduces Regular Expressions

According to the source code, the regular expressions lesson follows a linear progression designed for beginners. The content spans lines 54 through 456 of [`18_Day_Regular_expressions/18_regular_expressions.md`](https://github.com/Asabeneh/30-Days-Of-Python/blob/main/18_Day_Regular_expressions/18_regular_expressions.md), dividing the material into foundational API usage, advanced pattern construction, and practical problem sets.

### Module Import and Motivation

The lesson begins with the standard library import required for all pattern matching operations. As shown in lines 54-56, learners start with:

```python
import re

```

This single line provides access to Python’s full regular expression engine without external dependencies.

### The Five Essential re Functions

Lines 62-66 enumerate the core functions that form the basis of Python regex operations. The curriculum explains the distinct behavior of each:

- **`re.match()`** – Anchors the search to the beginning of the string only.
- **`re.search()`** – Scans the entire string to find the first occurrence.
- **`re.findall()`** – Returns a list of all non-overlapping matches.
- **`re.split()`** – Divides strings at pattern boundaries.
- **`re.sub()`** – Performs search-and-replace operations.

## Practical Code Examples from the Curriculum

The Day 18 material provides isolated code demonstrations for each function, allowing learners to observe return objects and match metadata.

### Matching at the Start with re.match()

Lines 70-92 demonstrate `re.match()` using a case-insensitive pattern flag. The example validates that a string starts with a specific phrase:

```python
txt = "I love to teach Python and JavaScript"
m = re.match(r"I love to teach", txt, re.I)
print(m)          # <re.Match object; span=(0, 15), match='I love to teach'>

print(m.span())   # (0, 15)

```

This snippet illustrates how match objects expose the `span()` method to retrieve match positions.

### Searching Anywhere with re.search()

To contrast with `match()`, lines 105-130 present `re.search()` operating on multi-line text. This function locates the first occurrence regardless of position:

```python
txt = """Python is awesome.
I recommend Python for beginners."""
s = re.search(r"Python", txt, re.I)
print(s)          # <re.Match object; span=(0, 6), match='Python'>

print(s.start())  # 0

```

### Extracting All Matches with re.findall()

Lines 134-146 introduce global pattern matching through `re.findall()`. The example extracts case variants of a word using the `re.I` flag:

```python
txt = "Python, python, and PYTHON are all the same."
all_python = re.findall(r"python", txt, re.I)
print(all_python)  # ['Python', 'python', 'PYTHON']

```

## Advanced Pattern Syntax and Text Substitution

Beyond basic function calls, the curriculum advances to pattern construction techniques in lines 150-210. Learners explore:

- **Case-insensitive matching** using the `re.I` flag.
- **Anchors** such as `^` (start of string) and `$` (end of string).
- **Character classes** and escape sequences like `\d` for digits and `\.` for literal periods.
- **Quantifiers** that control repetition bounds.

### Substitution with re.sub()

Lines 221-242 cover text transformation through `re.sub()`. The demonstration replaces all instances of a pattern with an alternative string:

```python
txt = "I love Python! Python is great."
clean = re.sub(r"Python", "JavaScript", txt)
print(clean)       # I love JavaScript! JavaScript is great.

```

## Hands-On Exercises for Skill Building

The lesson concludes with a tiered exercise set spanning lines 389-456. These assignments require learners to apply regex to realistic scenarios:

1. **Extract numeric values** from unstructured text.
2. **Validate variable names** using custom character class patterns.
3. **Clean noisy text** and compute word frequencies using `re.findall()` and `re.sub()`.

This progression moves from simple extraction to complex text preprocessing pipelines.

## Summary

- The Asabeneh/30-Days-Of-Python curriculum introduces regular expressions on **Day 18** in the file [`18_Day_Regular_expressions/18_regular_expressions.md`](https://github.com/Asabeneh/30-Days-Of-Python/blob/main/18_Day_Regular_expressions/18_regular_expressions.md).
- The lesson covers five core `re` module functions: `match()`, `search()`, `findall()`, `split()`, and `sub()`.
- Code examples demonstrate case-insensitive matching with `re.I`, match object inspection via `span()` and `start()`, and global pattern extraction.
- Advanced sections explain anchors, quantifiers, and escape sequences for building precise patterns.
- Three levels of hands-on exercises solidify skills in data extraction, validation, and text cleaning.

## Frequently Asked Questions

### On which day are regular expressions taught in the 30 Days of Python course?

Regular expressions are introduced on **Day 18** of the curriculum. The complete lesson resides in [`18_Day_Regular_expressions/18_regular_expressions.md`](https://github.com/Asabeneh/30-Days-Of-Python/blob/main/18_Day_Regular_expressions/18_regular_expressions.md), which contains the theoretical explanations, API documentation, and practice exercises.

### What specific re functions does the 30 Days of Python curriculum cover?

The curriculum details five primary functions from the `re` module: `re.match()` for start-anchored searches, `re.search()` for finding first occurrences anywhere, `re.findall()` for extracting all matches, `re.split()` for string division at patterns, and `re.sub()` for pattern-based substitution.

### Does the lesson include hands-on exercises for practicing regular expressions?

Yes, lines 389-456 of the Day 18 markdown file contain a structured exercise set. These tasks require learners to extract numbers from text, validate identifier names using regex, and sanitize messy data to perform word frequency analysis.

### Are pattern flags like case-insensitive matching explained in the 30 Days of Python regex lesson?

Absolutely. The lesson demonstrates the `re.I` flag (case-insensitive) in multiple examples, including the `re.match()` and `re.findall()` demonstrations on lines 70-92 and 134-146, respectively. The curriculum also covers additional pattern building blocks such as anchors and escape sequences.