# Regular Expressions in Python — the re Module, Patterns, Groups and Validation

Source: https://skills101.in/learn/python/python-regular-expressions — Skills101 free tutorial (Python Programming). Updated 2026-11-21.

Suppose you have a long text of student enquiries and you want every 10-digit mobile number in it, or you must check that a roll number looks like `22BCA045`. String methods like `find()` and `split()` cannot describe "10 digits" or "2 digits, then 3 capital letters, then 3 digits". A **regular expression** (regex) is a small pattern language that can. Python supports it through the built-in `re` module.

## 1. Your first pattern

```python
import re

text = "Call Asha on 9876543210 or the office on 9123456780."
print(re.findall(r"\d{10}", text))
```

```text
['9876543210', '9123456780']
```

`\d` means "one digit" and `{10}` means "exactly 10 times". The `r` before the string makes it a **raw string**, so the backslash reaches the regex engine unchanged. Always write patterns as raw strings; without `r`, some sequences such as `\b` are turned into other characters by Python before `re` sees them.

## 2. The building blocks

| Pattern | Matches | Example |
|---|---|---|
| `.` | any one character except a newline | `c.t` matches cat, cot |
| `\d` / `\D` | a digit / not a digit | `\d\d` matches 42 |
| `\w` / `\W` | a word character (letter, digit, `_`) / not one | `\w+` matches roll_no1 |
| `\s` / `\S` | whitespace (space, tab, newline) / not whitespace | |
| `[abc]` | one of a, b, c | `[aeiou]` matches a vowel |
| `[a-z]`, `[A-Z0-9]` | a range | |
| `[^0-9]` | anything **except** a digit | |
| `^` / `$` | start / end of the string | `^Hi` |
| `\b` | a word boundary | `\bcat\b` does not match "concat" |
| `a|b` | a or b | `cat|dog` |

**Quantifiers** say how many times the item before them repeats:

| Quantifier | Meaning |
|---|---|
| `*` | 0 or more |
| `+` | 1 or more |
| `?` | 0 or 1 (optional) |
| `{n}` | exactly n |
| `{m,n}` | from m to n |

To match a special character such as `.`, `+` or `(` literally, put a backslash before it: `\.` matches a real dot.

![The pattern for a roll number like 22BCA045 split into parts: two digits, then three capital letters, then three digits, each part labelled with what it matches](https://skills101.in/learn-img/python/regex-anatomy.svg)

## 3. search, match and fullmatch

These three check **one** place in the text and return a **match object**, or `None` if nothing matched.

| Function | Looks where? |
|---|---|
| `re.search(p, s)` | anywhere in s; first match |
| `re.match(p, s)` | only at the **start** of s |
| `re.fullmatch(p, s)` | the **whole** of s must match |

```python
import re

for s in ["22BCA045", "ID 22BCA045", "22BCA045X"]:
    print(s, "|",
          bool(re.search(r"\d{2}[A-Z]{3}\d{3}", s)),
          bool(re.match(r"\d{2}[A-Z]{3}\d{3}", s)),
          bool(re.fullmatch(r"\d{2}[A-Z]{3}\d{3}", s)))
```

```text
22BCA045 | True True True
ID 22BCA045 | True False False
22BCA045X | True True False
```

For **validation** (is this input correct?), use `fullmatch`. With `search` or `match`, extra characters before or after would be accepted by mistake, as the last row shows.

A match object tells you what matched and where:

```python
import re

m = re.search(r"\d+", "Room 204, Block C")
print(m.group(), m.start(), m.end(), m.span())
```

```text
204 5 8 (5, 8)
```

## 4. findall, finditer, sub and split

```python
import re

marks_text = "Maths 78, Physics 91, Chemistry 66"
print(re.findall(r"\d+", marks_text))

for m in re.finditer(r"[A-Z][a-z]+", marks_text):
    print(m.group(), "at", m.start())

print(re.sub(r"\s+", " ", "too    many   spaces"))
print(re.split(r"[,;|]\s*", "Pune, Delhi;Agra|Goa"))
```

```text
['78', '91', '66']
Maths at 0
Physics at 10
Chemistry at 22
too many spaces
['Pune', 'Delhi', 'Agra', 'Goa']
```

- `findall` returns a list of strings.
- `finditer` returns match objects one by one, which is useful when you also need positions.
- `sub(pattern, replacement, text)` replaces every match.
- `split` splits wherever the pattern matches, so it handles several separators at once.

![re.finditer on the text 'Maths 78, Physics 91, Chemistry 66' with the pattern \d+ highlights the three numbers 78, 91 and 66 and shows the start position of each match](https://skills101.in/learn-img/python/regex-finditer.svg)

## 5. Groups: pulling out parts

Round brackets `( )` make a **group**. Each group captures its own part of the match.

```python
import re

line = "2026-11-21 Asha scored 88"
m = re.fullmatch(r"(\d{4})-(\d{2})-(\d{2}) (\w+) scored (\d+)", line)
print(m.groups())
year, month, day, name, score = m.groups()
print(name, int(score) + 2)
```

```text
('2026', '11', '21', 'Asha', '88')
Asha 90
```

**Named groups** `(?P<name>...)` are easier to read:

```python
import re

m = re.search(r"(?P<user>[\w.]+)@(?P<domain>[\w.]+)", "Mail: ravi.k@college.edu")
print(m.group("user"), m.group("domain"))
print(m.groupdict())
```

```text
ravi.k college.edu
{'user': 'ravi.k', 'domain': 'college.edu'}
```

If a pattern has groups, `findall` returns the **groups** instead of the whole match. With one group you get a list of strings; with several, a list of tuples:

```python
import re

text = "Asha:91, Ravi:78, Neha:88"
print(re.findall(r"(\w+):(\d+)", text))
print(dict(re.findall(r"(\w+):(\d+)", text)))
```

```text
[('Asha', '91'), ('Ravi', '78'), ('Neha', '88')]
{'Asha': '91', 'Ravi': '78', 'Neha': '88'}
```

In `sub`, you can use groups in the replacement with `\1`, `\2`. This turns dates from YYYY-MM-DD into DD/MM/YYYY:

```python
import re

print(re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\3/\2/\1", "Exam on 2026-12-05"))
```

```text
Exam on 05/12/2026
```

## 6. Greedy and lazy matching

Quantifiers are **greedy**: they take as much text as they can. Adding `?` after a quantifier makes it **lazy**: it takes as little as possible.

```python
import re

html = "<b>Asha</b> and <b>Ravi</b>"
print(re.findall(r"<b>.*</b>", html))
print(re.findall(r"<b>.*?</b>", html))
print(re.findall(r"<b>(.*?)</b>", html))
```

```text
['<b>Asha</b> and <b>Ravi</b>']
['<b>Asha</b>', '<b>Ravi</b>']
['Asha', 'Ravi']
```

The greedy `.*` ran all the way to the **last** `</b>`, giving one long match. The lazy `.*?` stops at the first `</b>`.

## 7. Flags and compiling

Flags change how matching works. The common ones are `re.IGNORECASE` (`re.I`) and `re.MULTILINE` (`re.M`, where `^` and `$` match at every line).

If you use the same pattern many times, `re.compile` creates a pattern object once. It also gives the pattern a clear name.

```python
import re

MOBILE = re.compile(r"[6-9]\d{9}")

for number in ["9876543210", "1234567890", "98765"]:
    print(number, bool(MOBILE.fullmatch(number)))

print(re.findall(r"python", "Python, PYTHON and python", flags=re.I))
```

```text
9876543210 True
1234567890 False
98765 False
['Python', 'PYTHON', 'python']
```

Indian mobile numbers start with 6, 7, 8 or 9, which is why the pattern begins with `[6-9]`.

A simple email check, good enough for a form (real email rules are much more complex, so do not try to match every valid address with one regex):

```python
import re

EMAIL = re.compile(r"[\w.+-]+@[\w-]+(\.[\w-]+)+")
for e in ["asha@gmail.com", "ravi.k@college.ac.in", "neha@com", "imran@@mail.com"]:
    print(e, bool(EMAIL.fullmatch(e)))
```

```text
asha@gmail.com True
ravi.k@college.ac.in True
neha@com False
imran@@mail.com False
```

## Common mistakes

- Forgetting the `r` prefix, so backslashes are changed before `re` sees them.
- Using `search` or `match` for validation; extra characters slip through. Use `fullmatch`.
- Not escaping a dot: `.` matches any character, `\.` matches a real dot.
- Expecting `findall` to return whole matches when the pattern has groups.
- Using greedy `.*` where a lazy `.*?` was needed.
- Calling `.group()` on `None` when nothing matched. Check the result first: `if m:`.

## Quick recap

- `\d`, `\w`, `\s`, `[...]` and `.` match single characters; `*`, `+`, `?`, `{m,n}` repeat them.
- `search` looks anywhere, `match` only at the start, `fullmatch` needs the whole string.
- `findall`, `finditer`, `sub` and `split` work on every match.
- `( )` captures groups; `(?P<name>...)` names them; `\1` reuses them in `sub`.
- Add `?` after a quantifier for lazy matching; use `re.compile` for patterns you reuse.


Practice quiz and free certificate: https://skills101.in/learn/python/python-regular-expressions