Python Regular Expressions
Regular expressions (regex) in Python are powerful tools for pattern matching and manipulating text. The re
module in Python provides functions and methods for working with regular expressions. Here’s an overview of some common operations using regular expressions in Python:
- Importing the
re
module: Begin by importing there
module at the start of your Python script or interactive session:python
import re
Matching patterns: The re.match()
function is used to determine if the regex pattern matches at the beginning of a string:
python
pattern = r"abc" # Raw string notation for regex pattern
text = "abcdef"
match = re.match(pattern, text)
if match:
print("Match found!")
Searching for patterns: The re.search()
function searches the entire string for a match to the regex pattern:
python
pattern = r"world"
text = "Hello, world!"
match = re.search(pattern, text)
if match:
print("Match found!")
Finding multiple matches: The re.findall()
function returns all non-overlapping matches of the regex pattern in a string as a list:
python
pattern = r"\d+" # Match one or more digits
text = "I have 42 apples and 7 bananas."
matches = re.findall(pattern, text)
print(matches) # Output: ['42', '7']
Splitting a string: The re.split()
function splits a string by the occurrences of a regex pattern:
python
pattern = r"\s+" # Match one or more whitespace characters
text = "Hello World\tPython"
parts = re.split(pattern, text)
print(parts) # Output: ['Hello', 'World', 'Python']
Substituting patterns: The re.sub()
function replaces occurrences of a regex pattern in a string with a specified replacement:
python
pattern = r"\bapple\b" # Match the whole word "apple" text = "I have an apple and a pineapple." new_text = re.sub(pattern, "orange", text) print(new_text) # Output: "I have an orange and a pineapple."
These are just a few examples of what you can do with regular expressions in Python. Regular expressions can be quite complex, allowing you to specify advanced patterns and apply various modifiers to control matching behavior. The re
module in Python offers many more functions and options for working with regular expressions.