Regular Expressions, also called regex, are patterns used to search, match, and manipulate text. Python provides the built-in re module for working with regular expressions. Regular expressions can be used to find specific text patterns, validate input, extract information, replace text, and split strings. They are especially useful when working with emails, phone numbers, dates, usernames, and other structured text.
Table of Contents
What are Regular Expressions in Python?
A regular expression is a sequence of characters that defines a search pattern. Python provides the built-in re module to work with regular expressions.
Example:
\d+
can be used to find one or more digits in a string.
# Python program to find numbers using a regular expression import re text = "My marks are 85 and 92." result = re.findall(r"\d+", text) print(result)
Output:
['85', '92']
Here, \d represents a digit, and + means one or more occurrences.
Why Use Regular Expressions?
Regular expressions are useful when you need to search, extract, validate, or manipulate text based on specific patterns.
- Search for Specific Patterns: Regular expressions allow you to search text and find specific patterns, such as words, numbers, or particular sequences of characters.
- Validate User Input: They can be used to check whether user input follows a required format, such as an email address, phone number, or password pattern.
- Extract Information from Text: Regular expressions make it possible to find and extract specific information from a larger piece of text.
- Replace Matching Text: They can be used to find text that matches a particular pattern and replace it with another value.
- Split Text Based on Patterns: Regular expressions can split text into smaller parts based on a specified pattern.
- Process Structured Text: They are useful for processing text that follows a particular structure or format.
- Simplify Complex Text Processing: Regular expressions can reduce the amount of code needed for many text-searching and processing tasks.
Importing the re Module
Python provides the re module as part of its standard library, so it does not need to be installed separately.
The module can be imported using:
import re
Example:
# Python program to import the re module
import re
text = "Python is easy to learn."
result = re.search("Python", text)
print(result.group())
Output:
Python
Creating a Regular Expression Pattern
A regular expression pattern defines what we want to find in a string.
Example:
r"\d+"
Matches one or more digits. Using a raw string such as r"\d+" is recommended for regex patterns because it prevents Python from interpreting backslashes as regular string escape sequences.
# Python program to create a regular expression pattern import re text = "There are 25 students." pattern = r"\d+" result = re.findall(pattern, text) print(result)
Output:
['25']
Using re.search()
The re.search() function searches the entire string for the first location where the pattern matches.
Syntax:
re.search(pattern, string)
Example:
# Python program to use re.search()
import re
text = "Python is a programming language."
result = re.search(r"programming", text)
if result:
print("Pattern found")
else:
print("Pattern not found")
Output:
Pattern found
If the pattern is not found, re.search() returns None.
Using re.match()
The re.match() function checks for a match only at the beginning of the string.
Syntax:
re.match(pattern, string)
Example:
# Python program to use re.match()
import re
text = "Python is easy."
result = re.match(r"Python", text)
if result:
print("Match found")
else:
print("No match found")
Output:
Match found
If the pattern does not occur at the beginning, re.match() returns None.
Using re.findall()
The re.findall() function returns all non-overlapping matches as a list.
Syntax:
re.findall(pattern, string)
Example:
# Python program to use re.findall() import re text = "Python 3 is popular. Python is easy." result = re.findall(r"Python", text) print(result)
Output:
['Python', 'Python']
findall() is useful when all matching occurrences are required.
Using re.finditer()
The re.finditer() function returns an iterator containing match objects for all non-overlapping matches.
# Python program to use re.finditer()
import re
text = "Python is easy. Python is powerful."
matches = re.finditer(r"Python", text)
for match in matches:
print(match.group(), match.start())
Output:
Python 0
Python 17
The start() method returns the starting position of each match.
Using re.sub()
The re.sub() function replaces matching text with another string.
Syntax:
re.sub(pattern, replacement, string)
Example:
# Python program to replace text using re.sub() import re text = "I like Java." result = re.sub(r"Java", "Python", text) print(result)
Output:
I like Python.
Using re.split()
The re.split() function splits a string wherever the regular expression pattern matches.
# Python program to split text using re.split() import re text = "Apple, Banana; Mango Orange" result = re.split(r"[,; ]+", text) print(result)
Output:
['Apple', 'Banana', 'Mango', 'Orange']
Here, the pattern matches one or more commas, semicolons, or spaces.
Regular Expression Metacharacters
Metacharacters have special meanings in regular expressions.
| Metacharacter | Meaning |
|---|---|
| . | Matches any character except a newline by default |
| ^ | Matches the beginning of a string |
| $ | Matches the end of a string |
| * | Matches zero or more occurrences |
| + | Matches one or more occurrences |
| ? | Matches zero or one occurrence |
| {m} | Matches exactly m occurrences |
| {m,n} | Matches from m to n occurrences |
| [] | Defines a character class |
| () | Creates a group |
| ` | ` |
| \ | Gives special meaning to or escapes a character |
Example:
# Python program to use regular expression metacharacters import re text = "cat bat rat" result = re.findall(r"[cbr]at", text) print(result)
Output:
['cat', 'bat', 'rat']
Character Classes
Character classes are used to match one character from a specified set.
Some common character classes are:
[abc]
Matches a, b, or c.
[a-z]
Matches any lowercase letter from a to z.
[A-Z]
Matches any uppercase letter from A to Z.
[0-9]
Matches any digit from 0 to 9.
Example:
# Python program to use a character class import re text = "Python 123" result = re.findall(r"[A-Z]", text) print(result)
Output:
['P']
Special Sequences
Python provides several special sequences that make regular expressions easier to write.
| Sequence | Meaning |
|---|---|
| \d | Matches a digit |
| \D | Matches a non-digit |
| \w | Matches a word character |
| \W | Matches a non-word character |
| \s | Matches whitespace |
| \S | Matches non-whitespace |
| \b | Matches a word boundary |
| \B | Matches a position that is not a word boundary |
Example:
# Python program to use special sequences import re text = "Python 123" result = re.findall(r"\d+", text) print(result)
Output:
['123']
Quantifiers
Quantifiers specify how many times a pattern can occur.
| Quantifier | Meaning |
|---|---|
| * | Zero or more times |
| + | One or more times |
| ? | Zero or one time |
| {n} | Exactly n times |
| {n,} | At least n times |
| {n,m} | Between n and m times |
Example:
# Python program to use quantifiers
import re
text = "aa aaa aaaa"
result = re.findall(r"a{2,3}", text)
print(result)
Output:
['aa', 'aaa', 'aaa']
The pattern a{2,3} matches between two and three consecutive a characters.
Using Groups in Regular Expressions
Parentheses () are used to create groups in a regular expression. Groups are useful when we want to capture specific parts of a match.
# Python program to use groups in a regular expression
import re
text = "Name: Tanavi, Age: 21"
pattern = r"Name: (\w+), Age: (\d+)"
result = re.search(pattern, text)
if result:
print(result.group(1))
print(result.group(2))
Output:
Tanavi
21
Here, group(1) returns the name and group(2) returns the age.
Using Flags
Flags modify how a regular expression behaves. One commonly used flag is re. IGNORECASE, which makes matching case-insensitive.
# Python program to use the re.IGNORECASE flag
import re
text = "Python is powerful."
result = re.search(r"PYTHON", text, re.IGNORECASE)
if result:
print("Pattern found")
Output:
Pattern found
Other commonly used flags include re.MULTILINE, re.DOTALL, and re.VERBOSE.
Advantages of Regular Expressions
- Powerful Text Searching: Regex can find complex patterns in text.
- Input Validation: It can validate formats such as emails, phone numbers, and dates.
- Data Extraction: Specific information can be extracted from large text.
- Text Replacement: Matching text can be replaced easily.
- Flexible: Regular expressions provide many patterns and options for text processing
- Built-in Support: Python provides the re module as part of its standard library.
Common Beginner Mistakes
1. Forgetting to Import the re Module: Import the re module before using functions such as re.search() or re.findall().
2. Confusing re.match() and re.search(): re.match() checks from the beginning of the string, while re.search() searches throughout the string.
3. Forgetting the Raw String Prefix: Using raw strings such as r"\d+" makes regex patterns easier to write and avoids many backslash-related issues.
4. Using the Wrong Quantifier: Make sure to understand the difference between *, +, ?, and {m,n}.
5. Expecting findall() to Return Match Objects: re.findall() normally returns matching strings, while re.finditer() returns an iterator of match objects.
Conclusion
Regular Expressions in Python provide a powerful way to search, match, extract, replace, and split text based on patterns. Python's built-in re module provides functions such as search(), match(), findall(), finditer(), sub(), and split() for working with regular expressions. Understanding metacharacters, character classes, special sequences, quantifiers, and groups helps developers perform effective text processing and validation.
Frequently Asked Questions
1. What are Regular Expressions in Python?
Regular expressions are patterns used to search, match, extract, replace, and manipulate text.
2. Which module is used for Regular Expressions in Python?
Python provides the built-in re module for working with regular expressions.
3. What is the difference between re.match() and re.search()?
re.match() checks for a match at the beginning of the string, while re.search() searches for a match anywhere in the string.
4. What does \d mean in a regular expression?
\d matches a digit from 0 to 9.
5. Which function is used to replace text using Regular Expressions?
The re.sub() function is used to replace text that matches a specified regular expression pattern.
0 Comments