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 expressionMatchObject
: 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 the operations that can be done, such as matching a pattern and finding all substrings that match a particular regex. So, for example, if we want to know if a string starts with <HTML>
, we can use the following code:
>>> pattern...