Parsing Information With Regular Expressions

Learn about the functionalities of regular expressions using the re module.

Let’s now focus on the Python side of things. The regular expression syntax is the furthest thing from object-oriented programming. However, Python’s re module provides an object-oriented interface to enter the regular expression engine.

We’ve been checking whether the re.match() function returns a valid object or not. If a pattern does not match, that function returns None. If it does match, however, it returns a useful object that we can inspect for information about the pattern.

So far, our regular expressions have answered questions such as does this string match this pattern? Matching patterns is useful, but in many cases, a more interesting question is if this string matches this pattern, what is the value of a relevant substring? If we use groups to identify parts of the pattern that we want to reference later, we can get them out of the match return value, as illustrated in the next example:

Get hands-on with 1400+ tech skills courses.