Building blocks for Python regex

In Python, there are two different objects dealing with Regex:

  • RegexObject: It is also known as Pattern Object. It represents a compiled regular expression
  • MatchObject: It represents the matched pattern


In order to start matching patterns, we'll have to compile the regex. Python gives us an interface to do that as we've seen previously. The result will be a pattern object or RegexObject. This object has several methods for typical operations on regular expressions. As we will see later, the re module provides a shorthand for every operation so that we can avoid compiling it first.

>>> pattern = re.compile(r'fo+')

The compilation of a regular expression produces a reusable pattern object that provides all ...

Get Mastering Python Regular Expressions now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.