Overview
Regular expressions define patterns for string processing. They serve two primary purposes:
- Matching - Determining whether a string conforms to a specified pattern
- Extracting - Pulling specific portions from a string that match the pattern
The Python standard library provides the re module for handling regular expressions.
Core Methods
re.match(pattern, string) examines the string from its beginning. If the first character fails to match the pattern, the operation fails immediate.
re.search(pattern, string) finds the first occurrence anywhere within the string. It does not require matching from the start position.
re.findall(pattern, string) returns all matching substrings as a list.
re.sub(pattern, replacement, string) replaces all occurrences of the pattern with the specified replacement text.
Common Pattern Tokens
Wildcard: .
Matches any single character except newline (\n).
import re
text = "hello123world!!"
result = re.search('.', text)
print(result) #<_sre.SRE_Match object; span=(0, 1), match='h'>
Start Anchor: ^
Asserts that the following pattern must appear at the beginning of the string.
import re
text = "hello123world!!"
result = re.search('^h', text)
print(result) #<_sre.SRE_Match object; span=(0, 1), match='h'>
failed = re.search('^F', text) # Returns None - string does not start with 'F'
Since re.match() always searches from the beginning, using ^ with match() produces equivalent behavior:
import re
text = "hello123world!!"
print(re.match('h', text)) #<_sre.SRE_Match object; span=(0, 1), match='h'>
print(re.match('F', text)) #None
End Anchor: $
Asserts that the preceding pattern must appear at the end of the string.
import re
text = "hello123world!!"
print(re.search('!$', text)) #<_sre.SRE_Match object; span=(14, 15), match='!'>
print(re.search('D$', text)) #None
Quantifiers
* matches the preceding element zero or more times.
+ matches the preceding element one or more times.
import re
text = "hello123world!!"
print(re.search('ll*', text)) #<_sre.SRE_Match object; span=(2, 4), match='ll'>
# The asterisk allows the second 'l' to match zero additional times
print(re.search('ll+', text)) #None
# Plus requires at least two 'l' characters, but only two exist here
{m} specifies an exact count of repetitions.
import re
text = "hello123world!!"
print(re.search('el{2}', text)) #<_sre.SRE_Match object; span=(1, 4), match='ell'>
# Matches exactly two 'l' characters following 'e'
print(re.search('el{1}', text)) #<_sre.SRE_Match object; span=(1, 3), match='el'>
# Matches a single 'l' following 'e'
print(re.search('el{3}', text)) #None
# Requires three 'l' characters, which do not exist
? matches the preceding element zero or one time.
import re
text = "hello123world!!"
print(re.search('el?', text)) #<_sre.SRE_Match object; span=(1, 3), match='el'>
# The 'l' can appear zero or one time
print(re.search('ello7?', text)) #<_sre.SRE_Match object; span=(1, 5), match='ello'>
# The '7' is optional, so it matches without the trailing digit