Regular Expressions

Regular Expressions (RegEx)

What are Regular Expressions?

Regular expressions, often abbreviated as regex or regexp, are sequences of characters that define a search pattern. They are used for pattern matching within strings, allowing you to search for specific patterns of text or extract specific information from text data. Regular expressions are a powerful tool for text processing and manipulation, enabling you to perform complex search and replace operations with ease.

In Python, regular expressions are supported through the re module, which provides functions for working with regular expressions. The re module allows you to compile regular expressions into pattern objects, search for matches within strings, and perform various operations on the matched text.

Basic Pattern Matching

To use regular expressions in Python, you need to import the re module. Here’s an example of a simple regular expression pattern that matches the word “Python” in a text string:

regex_basic.py
import re

# Define the text string
text = "Python is a popular programming language."

# Define the regular expression pattern
pattern = r"Python"

# Search for the pattern in the text string
match = re.search(pattern, text)

# Check if the pattern is found
if match:
    print("Pattern found in the text.")
else:
    print("Pattern not found.")

In this example, the regular expression pattern r"Python" is compiled using the re.search() function to search for the word “Python” in the text string. If the pattern is found, the message “Pattern found in the text.” is printed; otherwise, the message “Pattern not found.” is printed.

Searching and Matching Patterns

Regular expressions support a wide range of pattern matching capabilities, allowing you to search for specific sequences of characters, words, or patterns within text data. Here are some common functions provided by the re module for working with regular expressions:

FunctionDescription
re.search(pattern, string)Searches for the first occurrence of the pattern in the string.
re.match(pattern, string)Matches the pattern at the beginning of the string.
re.findall(pattern, string)Finds all occurrences of the pattern in the string.
re.split(pattern, string)Splits the string based on the pattern.
re.sub(pattern, replacement, string)Replaces occurrences of the pattern with the replacement in the string.
re.compile(pattern)Compiles the pattern into a pattern object for reuse.
pattern.search(string)Searches for the pattern in the string using a compiled pattern object.
pattern.match(string)Matches the pattern at the beginning of the string using a compiled pattern object.
pattern.findall(string)Finds all occurrences of the pattern in the string using a compiled pattern object.
pattern.split(string)Splits the string based on the pattern using a compiled pattern object.
pattern.sub(replacement, string)Replaces occurrences of the pattern with the replacement in the string using a compiled pattern object.
pattern.finditer(string)Finds all occurrences of the pattern in the string and returns an iterator of match objects.
pattern.fullmatch(string)Matches the entire string against the pattern using a compiled pattern object.
pattern.group()Returns the matched text from the string.
pattern.groups()Returns a tuple of matched groups from the string.
pattern.groupdict()Returns a dictionary of named matched groups from the string.
pattern.start()Returns the start position of the matched text.
pattern.end()Returns the end position of the matched text.
pattern.span()Returns a tuple of the start and end positions of the matched text.
pattern.flagsReturns the flags used to compile the pattern.
pattern.patternReturns the regular expression pattern.

Advanced Pattern Matching

Regular expressions support a wide range of pattern matching features, including character classes, quantifiers, anchors, groups, and more. Here are some common elements used in regular expressions:

ElementDescription
.Matches any character except a newline.
\dMatches any digit (0-9).
\DMatches any non-digit character.
\wMatches any word character (alphanumeric and underscore).
\WMatches any non-word character.
\sMatches any whitespace character.
\SMatches any non-whitespace character.
[abc]Matches any character in the set (a, b, or c).
[^abc]Matches any character not in the set (a, b, or c).
a*Matches zero or more occurrences of the character a.
a+Matches one or more occurrences of the character a.
a?Matches zero or one occurrence of the character a.
a{3}Matches exactly three occurrences of the character a.
a{3,}Matches three or more occurrences of the character a.
a{3,5}Matches between three and five occurrences of the character a.
^Matches the start of the string.
$Matches the end of the string.
\bMatches a word boundary.
\BMatches a non-word boundary.
()Groups multiple elements together.
|Matches either the pattern on the left or the pattern on the right.
(?P<name>...)Named capture group.
(?=...)Positive lookahead assertion.
(?!...)Negative lookahead assertion.
(?<=...)Positive lookbehind assertion.
(?<!...)Negative lookbehind assertion.
(?i)Case-insensitive matching.
(?s)Dot matches all, including newline.
(?m)Multi-line mode.
(?x)Verbose mode.
(?a)ASCII-only mode.
(?L)Locale-dependent mode.
(?u)Unicode mode.
(?x:...)Inline verbose mode.
(?iLmsux)Flags for case-insensitive, locale-dependent, multi-line, dot-all, Unicode, and verbose modes.
(?-iLmsux)Turn off flags for case-insensitive, locale-dependent, multi-line, dot-all, Unicode, and verbose modes.
(?#...)Comment.

Regular expressions provide a powerful and flexible way to search for and manipulate text data in Python. By mastering regular expressions, you can perform complex text processing tasks with ease and efficiency.

Example: Extracting Email Addresses

Here’s an example that demonstrates how to extract email addresses from a text string using regular expressions in Python:

regex_email.py
import re

# Define the text string
text = "Contact us at [email protected] or [email protected]"

# Define the regular expression pattern for email addresses

pattern = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"

# Find all email addresses in the text string
emails = re.findall(pattern, text)

# Print the extracted email addresses

for email in emails:
    print(f"{email=}")

Resources and Further Learning

To learn more about regular expressions in Python, refer to the official Python documentation on the re module. You can also explore online resources and tutorials to deepen your understanding of regular expressions and their applications in text processing.

Can I put Regular Expressions in a Regular Expression?

Yes
Yes

Summary

Regular expressions (regex) are sequences of characters that define a search pattern for text processing and manipulation. In Python, regular expressions are supported through the re module, which provides functions for working with regular expressions. Regular expressions allow you to search for specific patterns of text, extract information from text data, and perform complex search and replace operations. By mastering regular expressions, you can enhance your text processing capabilities and perform a wide range of text manipulation tasks with ease.