Sequence of characters that forms a search pattern.
Regular expressions (regex) are a powerful tool in Python, used for pattern matching, substitution, and parsing in strings. This unit will delve into these three key applications of regex.
Pattern matching is one of the most common uses of regular expressions. It involves identifying whether a particular pattern exists within a given string or finding all instances of a pattern in a string.
Python's re
module provides several functions for pattern matching, including match()
, search()
, and findall()
.
match()
: This function checks for a match only at the beginning of the string.search()
: This function searches the string for a match and returns a match object if found.findall()
: This function returns all non-overlapping matches of a pattern in a string as a list of strings.Here's an example of pattern matching using findall()
:
import re text = "The rain in Spain" x = re.findall("ai", text) print(x) # Output: ['ai', 'ai']
Substitution is another common use of regular expressions. It involves replacing parts of a string that match a particular pattern.
The sub()
function in the re
module is used for substitution. It replaces all occurrences of the pattern in the string with a substitute and returns the modified string.
Here's an example of substitution:
import re text = "The rain in Spain" x = re.sub("ai", "oo", text) print(x) # Output: "The roon in Spoon"
Parsing is a more complex application of regular expressions. It involves extracting specific information from a string by identifying patterns.
For example, you might want to extract all email addresses from a text. You can do this by defining a pattern that matches the structure of an email address and using the findall()
function to extract all matches.
Here's an example of parsing:
import re text = "Contact us at: info@example.com, sales@example.net" emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text) print(emails) # Output: ['info@example.com', 'sales@example.net']
In conclusion, regular expressions are a powerful tool for pattern matching, substitution, and parsing in Python. With a solid understanding of regex, you can perform complex string manipulations with ease.