Published on

Regular Expressions in Python

Authors

Overview

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. Some of the important functions from re module are:

re.compile

Compiles a regular expression pattern into a regular expression object, which can be used for matching using match(), search() and other methods.

Syntax

re.compile(pattern, flags=0)

Examples

  1. Compile a pattern for matching a numerical value.
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'>
  1. Compile a pattern for with specifying flags.
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']

re.match

Check for pattern match at the beginning of string. In case of match return a match object else return None.

Syntax

re.match(pattern, string, flags=0)

Examples

  1. Perform match operation on string.
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

re.search

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.

Syntax

re.search(pattern, string, flags=0)

Examples

  1. Perform search operation on string.
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'>

re.findall

Scans a string left to right and returns all non-overlapping matches of defined pattern in string.

Syntax

re.findall(pattern, string, flags=0)

Examples

  1. Perform findall operation on string.
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']

re.finditer

Scans a string left to right and returns an iterator yielding match objects over all non-overlapping matches for the pattern in string.

Syntax

re.finditer(pattern, string, flags=0)

Examples

  1. Perform finditer operation on 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'>

re.sub

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

Syntax

re.sub(pattern, repl, string, count=0, flags=0)

Examples

  1. 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.