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

RegexObject

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.