---
title: "Explain the Python RegEx with Example"  
description: "Explain the Python RegEx with Example"  
author: "ICSM Computer"  
published: 2025-10-12  
updated: 2025-10-14  
canonical: https://www.mindstick.com/forum/161956/explain-the-python-regex-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 4 minutes  

---

# Explain the Python RegEx with Example

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) RegEx with Example**

## Replies

### Reply by Mayank kumar Verma

Regex is a super smart search tool to finding patterns in text like email, phone number etc.

Basic stepup

pythone

import re

## 4 Main Functions You Need

**1 Find if exists -** re. search ()

Python

text = “My number is 9876543210”

## 2 Find ALL

python

re.findall(r'\d+', “I have 2 cats, 3 dogs”) # ['2', ‘3’,]

## 3 Replace

python

re.sub(r'\d', 'X', "Call 123") *# Call XXX*

## Key Patterns:

\d = digit (0-9)

\w = letter

\s = space

+ = one or more

## Example

python

emport re

text = "Email: john@site.com"

email = re.findall(r'\S+@\S+', text)

print(email) #[ ‘jhon@site.com’ ]

Done.

### Reply by Anubhav Sharma

> ## What is RegEx?
>
> **RegEx (Regular Expression)** is a [**pattern-matching language**](https://www.mindstick.com/interview/34387/explain-the-python-modules-with-example) used to find, search, or manipulate text (strings).

Python has a built-in [module for RegEx](https://www.mindstick.com/articles/1092/regular-expression):

```python
import re
```

## Common RegEx Functions in Python (`re` module)

| Function | Description |
| --- | --- |
| `re.match()` | Matches pattern **only at the beginning** of a string |
| `re.search()` | Searches for a pattern **anywhere** in the string |
| `re.findall()` | Returns **all matches** in a list |
| `re.finditer()` | Returns **iterator** of match objects |
| `re.sub()` | Replaces matches with another string |
| `re.split()` | Splits a string by the pattern |

## Basic Syntax

| Symbol | Meaning | Example | Matches |
| --- | --- | --- | --- |
| `.` | Any character (except newline) | `a.c` | “abc”, “axc” |
| `^` | Start of string | `^Hello` | “Hello world” |
| `$` | End of string | `world$` | “Hello world” |
| `*` | 0 or more repetitions | `go*d` | “gd”, “god”, “good” |
| `+` | 1 or more repetitions | `go+d` | “god”, “good” |
| `?` | 0 or 1 repetition | `go?d` | “gd”, “god” |
| `{n}` | Exactly n times | `\d{3}` | “123” |
| `{n,m}` | Between n and m times | `\d{2,4}` | “12”, “1234” |
| `[]` | Any one of listed characters | `[aeiou]` | Any vowel |
| `\d` | Any digit | `\d` | “0–9” |
| `\w` | Any word char (a–z, A–Z, 0–9, _) | `\w+` | “hello123” |
| `\s` | Any whitespace | `\s` | “ ”, “\t” |
| ` | ` | OR | `cat |
| `()` | Group | `(ab)+` | “abab” |

## Examples

### 1. Search for a Word

```python
import re

text = "Python is powerful"
match = re.search("powerful", text)

if match:
    print("Found:", match.group())
```

## Output:

```plaintext
Found: powerful
```

### 2. Check if String Starts with “Hello”

```python
import re

text = "Hello World"
if re.match("^Hello", text):
    print("Starts with Hello")
```

## Output:

```plaintext
Starts with Hello
```

### 3. Find All Numbers

```python
import re

text = "Order numbers: 123, 456, and 789"
numbers = re.findall(r"\d+", text)
print(numbers)
```

## Output:

```plaintext
['123', '456', '789']
```

### 4. Replace Text

```python
import re

text = "I love Java"
result = re.sub("Java", "Python", text)
print(result)
```

## Output:

```plaintext
I love Python
```

### 5. Split by Non-Alphabetic Characters

```python
import re

text = "apple,banana;orange|grape"
fruits = re.split(r"[,;|]", text)
print(fruits)
```

## Output:

```plaintext
['apple', 'banana', 'orange', 'grape']
```

### 6. Validate Email Example

```python
import re

email = "user.name@example.com"
pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"

if re.match(pattern, email):
    print("Valid email")
else:
    print("Invalid email")
```

## Output:

```plaintext
Valid email
```

## Match Object Details

If a match is found using `re.search()` or `re.match()`, it returns a **match object**.

Example:

```python
import re

text = "My number is 9876543210"
match = re.search(r"\d+", text)

if match:
    print("Matched text:", match.group())
    print("Start index:", match.start())
    print("End index:", match.end())
```

## Output:

```plaintext
Matched text: 9876543210
Start index: 13
End index: 23
```

## Summary

| Use Case | Function | Example |
| --- | --- | --- |
| Find a word | `re.search()` | `re.search("word", text)` |
| Find all matches | `re.findall()` | `re.findall(r"\d+", text)` |
| Replace text | `re.sub()` | `re.sub("old", "new", text)` |
| Split text | `re.split()` | `re.split(r"\s+", text)` |
| Validate pattern | `re.match()` | `re.match(r"^[A-Za-z]+$", text)` |


---

Original Source: https://www.mindstick.com/forum/161956/explain-the-python-regex-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
