A regular expression (or RE) specifies a set of strings that matches it. the functions in this module let you check if a particular string matches a given regular expression. Let's see how to use some of the most important functions from re module.
Compiles a regular expression pattern into a regular expression object, which can be used for matching using `match()`, `search()` and other methods.
re.compile(pattern, flags=0)
import re
# define pattern
pattern = r'(\d+)'
# define sample string
foo = "sample text having numerical value 1234"
# compile pattern
prog = re.compile(pattern)
# perform match or search operation
result = prog.search(foo)
print(result)
<re.Match object; span=(35, 39), match='1234'>
import re
# define pattern
pattern = r'(abc)'
# define sample string
foo = "Find occurrence of both ABC and abc."
# compile pattern with ignoring case
prog = re.compile(pattern, flags=re.IGNORECASE)
# perform match or search operation
result = prog.findall(foo)
print(result)
['ABC', 'abc']
Check for pattern match at the beginning of string. In case of match return a match object else return `None`.
re.match(pattern, string, flags=0)
import re
# define pattern
pattern = r'(ABC)'
# define sample strings
foo = "ABC will be located by re.match in this string."
bar = "re.match won't locate ABC in this string."
# perform match on foo and bar
res_1 = re.match(pattern, foo)
res_2 = re.match(pattern, bar)
print(res_1)
print(res_2)
<re.Match object; span=(0, 3), match='ABC'>
None
Check complete string for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return `None` if no position in the string matches the pattern.
re.search(pattern, string, flags=0)
import re
# define pattern
pattern = r'(ABC)'
# define sample strings
foo = "ABC will be located by re.search in this string."
bar = "re.search will locate ABC anywhere in the string."
# perform match on foo and bar
res_1 = re.search(pattern, foo)
res_2 = re.search(pattern, bar)
print(res_1)
print(res_2)
<re.Match object; span=(0, 3), match='ABC'>
<re.Match object; span=(22, 25), match='ABC'>
Scans a string left to right and returns all non-overlapping matches of defined pattern in string.
re.findall(pattern, string, flags=0)
import re
# define pattern
pattern = r'(\d+)'
# define sample strings
foo = "Found numerical values like 123, 343 in the string."
# perform findall on foo
result = re.findall(pattern, foo)
print(result)
['123', '343']
Scans a string left to right and returns an iterator yielding match objects over all non-overlapping matches for the pattern in string.
import re
# define pattern
pattern = r'(\d+)'
# define sample strings
foo = "Found numerical values like 123, 343 in the string."
# perform finditer on foo
result = re.finditer(pattern, foo)
# check type of result
print(type(result))
# print match results
for m in result:
print(m)
<re.Match object; span=(28, 31), match='123'>
<re.Match object; span=(33, 36), match='343'>
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement `repl`.
re.sub(pattern, repl, string, count=0, flags=0)
Perform substitution operation on string.
import re
# define pattern
pattern = r'(\d+)'
# define sample strings
foo = "Replace 123 with some letters using re.sub."
# perform finditer on foo
new_str = re.sub(pattern=pattern, repl="ABC", string=foo)
print(new_str)
Replace ABC with some letters using re.sub.
Category: Python