Python regular expressions (regex) provide a powerful way to search, match, extract, and manipulate text using patterns. Whether you're validating input, parsing logs, or cleaning data, regex helps automate complex string operations efficiently. In this guide, you’ll learn the most commonly used regex patterns in Python with practical examples.
Python Regex Syntax - Quick Cheat Sheet
| Pattern | Meaning | Example | Matches |
|---|---|---|---|
. | Any character except newline | a.c | abc, a1c |
^ | Start of string | ^Hello | Hello world |
$ | End of string | end$ | the end |
\d | Digit (0–9) | \d+ | 123 |
\D | Non-digit | \D+ | abc |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ | test_1 |
\W | Non-word character | \W+ | @#! |
\s | Whitespace | \s+ | space, tab |
\S | Non-whitespace | \S+ | text123 |
[] | Character set | [aeiou] | a, e |
[^] | Negated set | [^0-9] | a, b |
| ` | ` | OR operator | `cat |
() | Grouping | (ab)+ | ab, abab |
* | 0 or more | a* | "", a, aaa |
+ | 1 or more | a+ | a, aaa |
? | 0 or 1 | colou?r | color, colour |
{n} | Exactly n times | a{3} | aaa |
{m,n} | Between m and n | a{2,4} | aa, aaa |
{m,} | At least m times | a{2,} | aa, aaa |
\b | Word boundary | \bcat\b | cat (not scatter) |
\B | Non-word boundary | \Bcat | scatter |
\A | Start of string (strict) | \AHi | Hi there |
\Z | End of string (strict) | end\Z | the end |
Common Python re Functions
| Function | Description | Example |
|---|---|---|
re.search() | Find first match anywhere | re.search(r'\d+', s) |
re.match() | Match only at beginning | re.match(r'\d+', s) |
re.findall() | Find all matches | re.findall(r'\d+', s) |
re.sub() | Replace matches | re.sub(r'\s+', ' ', s) |
re.split() | Split string by pattern | re.split(r'\s+', s) |
re.compile() | Compile regex for reuse | re.compile(r'\d+') |
Common Real-World Patterns
| Use Case | Pattern | Example |
|---|---|---|
| Email validation | [\w\.-]+@[\w\.-]+ | 'test@mail.com' |
| Phone number | \d{3}-\d{3}-\d{4} | 123-456-7890 |
| Extract numbers | \d+ | 45, 100 |
| Remove extra spaces | \s+ | "a b" → "a b" |
| Extract words | \w+ | hello, world |
Basic Python Regex Examples (Beginner Friendly)
Match a word or pattern in a string
In Python regex, matching a word or pattern in a string is one of the most common tasks. The re.search() function scans the entire string and returns the first match, making it useful for pattern detection, validation, and text analysis.
import re
text = "Learning python regex is fun"
pattern = r'python'
match = re.search(pattern, text)
if match:
print("Match found:", match.group())Output:
Match found: pythonFind all matches using re.findall
To extract multiple values from a string, Python provides the re.findall() function. It returns all matching patterns, making it ideal for data extraction, parsing logs, and retrieving structured values.
import re
text = "Order IDs: 123, 456, 789"
pattern = r'\d+'
result = re.findall(pattern, text)
print(result)Output:
['123', '456', '789']Search vs match in Python regex
Understanding the difference between re.search() and re.match() is essential when working with Python regex. While re.search() scans the entire string, re.match() only checks from the beginning, which is useful for input validation and strict pattern matching.
re.search()→ scans entire stringre.match()→ checks only beginning
import re
text = "python is powerful"
print(re.search(r'python', text)) # Match
print(re.match(r'python', text)) # Match
text2 = "learn python"
print(re.search(r'python', text2)) # Match
print(re.match(r'python', text2)) # No matchPython Regex Character Classes and Patterns
Digits, words and whitespace (\d, \w, \s)
Character classes like \d, \w, and \s are shortcuts used in Python regex to match digits, words, and whitespace.
import re
text = "User123 logged in"
print(re.findall(r'\d+', text)) # Digits
print(re.findall(r'\w+', text)) # Words
print(re.findall(r'\s', text)) # SpacesCustom character sets using []
Square brackets [] allow you to define a custom set of characters to match. This is useful when you want to match specific patterns or multiple possible characters at a certain position.
import re
text = "cat bat mat rat"
# Match words starting with c or b
print(re.findall(r'[cb]at', text))Output:
['cat', 'bat']Anchors (^ and $) explained with examples
Anchors such as ^ (start) and $ (end) are used to match patterns at specific positions in a string. They are commonly used in validation scenarios like checking full strings, file formats, or command outputs.
import re
text = "hello world"
# Starts with hello
print(re.search(r'^hello', text))
# Ends with world
print(re.search(r'world$', text))Python Regex Groups and Capturing Data
Using parentheses () for grouping
Grouping using parentheses () allows you to treat multiple parts of a regex as a single unit and capture matched values.
import re
text = "abc123xyz"
pattern = r'(abc)(123)'
match = re.search(pattern, text)
print(match.groups())Output:
('abc', '123')Extract values using groups
Regex groups can be used to extract structured data such as dates, IDs, or formatted strings.
import re
text = "Date: 2026-03-17"
pattern = r'(\d{4})-(\d{2})-(\d{2})'
match = re.search(pattern, text)
year, month, day = match.groups()
print(year, month, day)Named groups in Python regex
Named groups improve readability by assigning meaningful names to captured values.
import re
text = "Price: 499 INR"
pattern = r'(?P<amount>\d+)'
match = re.search(pattern, text)
print(match.group('amount'))Python Regex Practical Use Cases (Real Scenarios)
Extract numbers from log files
Extracting numbers from logs is a common real-world use case of Python regex. This technique is widely used in log analysis, monitoring systems, debugging, and parsing application output where numerical values like error codes, timestamps, or IDs need to be captured.
import re
log = "Error code 500 at 10:45"
numbers = re.findall(r'\d+', log)
print(numbers)Output:
['500', '10', '45']Validate email format using regex
Validating email addresses using Python regex is a common requirement in form validation, user input processing, and web applications. Regex helps ensure that the email follows a standard format before storing or processing it.
import re
email = "user@example.com"
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
if re.match(pattern, email):
print("Valid email")Validate phone numbers using regex
Phone number validation is another practical use case of regex, especially in data validation, APIs, and registration forms. Regex ensures the number follows a specific format such as XXX-XXX-XXXX.
import re
phone = "987-654-3210"
pattern = r'^\d{3}-\d{3}-\d{4}$'
if re.match(pattern, phone):
print("Valid phone number")Parse configuration or command output
Regex is very useful for extracting structured data from configuration files or command outputs. This is commonly used in DevOps, automation scripts, system administration, and parsing CLI outputs.
import re
config = "IP=192.168.1.10"
pattern = r'(\d+\.\d+\.\d+\.\d+)'
ip = re.search(pattern, config).group()
print(ip)Output:
192.168.1.10Python Regex Replace and Split Examples
Replace text using re.sub
Use re.sub() to replace matching patterns in a string.
import re
text = "This is a test"
result = re.sub(r'\s+', ' ', text)
print(result)Output:
This is a testSplit strings using regex patterns
Use re.split() to split a string using a pattern instead of a fixed delimiter.
import re
text = "apple,orange;banana grape"
result = re.split(r'[,\s;]+', text)
print(result)Output:
['apple', 'orange', 'banana', 'grape']Clean unwanted characters from text
Remove special characters and keep only alphanumeric values.
import re
text = "Hello@# World!! 123"
cleaned = re.sub(r'[^\w\s]', '', text)
print(cleaned)Output:
Hello World 123Python Regex Performance Tips
When to use re.compile
Use re.compile() when the same pattern is reused multiple times.
import re
pattern = re.compile(r'\d+')
text = "Order 123, Ref 456"
print(pattern.findall(text))Avoiding common regex mistakes
- Avoid overly complex patterns when simple string methods work
- Always test regex with sample input
- Use raw strings (
r'') to prevent escape issues
pattern = r'\d+' # Correct
pattern = '\\d+' # Also works but less readableGreedy vs non-greedy matching
- Greedy (
.*) → matches as much as possible - Non-greedy (
.*?) → matches as little as possible
import re
text = "<tag>content</tag>"
# Greedy
print(re.findall(r'<.*>', text))
# Non-greedy
print(re.findall(r'<.*?>', text))Output:
['<tag>content</tag>']
['<tag>', '</tag>']Common Python Regex Mistakes (Important for Beginners)
Difference between re.match and re.search
import re
text = "learn python"
print(re.match(r'python', text)) # None
print(re.search(r'python', text)) # Matchre.match()→ checks only beginningre.search()→ scans entire string
Escaping special characters properly
Special characters must be escaped using \.
import re
text = "price is $100"
pattern = r'\$100'
print(re.search(pattern, text))Case sensitivity issues
Regex is case-sensitive by default. Use re.IGNORECASE when needed.
import re
text = "Python"
print(re.search(r'python', text)) # No match
print(re.search(r'python', text, re.IGNORECASE)) # MatchFrequently Asked Questions
1. What is Python regex used for?
Python regex is used to search, match, extract, and manipulate text based on patterns. It is commonly used for validation, parsing logs, and data cleaning.2. What is the difference between re.match and re.search?
re.match checks for a match only at the beginning of the string, while re.search scans the entire string for the first occurrence of the pattern.3. What is re.findall in Python?
re.findall returns a list of all matching patterns found in a string, making it useful for extracting multiple values such as numbers or words.4. When should I use re.compile in Python?
re.compile should be used when the same regex pattern is reused multiple times, as it improves performance by compiling the pattern once.5. How to make regex case insensitive in Python?
You can use the re.IGNORECASE flag in functions like re.search or re.match to perform case-insensitive matching.Summary
Python regex is a powerful tool for text processing, allowing you to search, extract, replace, and validate data efficiently. By understanding core patterns, functions like re.search, re.findall, and re.sub, and avoiding common mistakes, you can handle most real-world string manipulation tasks with ease.










![Pandas Pivot Simplified [In-Depth Tutorial]](/pandas-pivot/python-pandas-pivot_hu_dc49d2c8eb5c965b.webp)