Regular Expression in Python – Set 2:

The re module supports for regular expressions in Python, we will discuss the main methods in this module in detail in this article with relate to data science.

Searching occurrence of pattern: When we use this method it either returns None (in case the pattern does not match) or a re.MatchObject which contains information about matching part of the string. This method comes to a stop after there is a first match. This method is best for testing a regular expression rather than extracting data.

Regular Expression in Python for Data Science – Set 2 - PST Analytics

Matching Pattern with Text:

re.match(): This is a function which attempts to match a pattern to a whole string. It returns a match object when it finds one and None in case it fails.

Regular Expression in Python for Data Science – Set 2 - PST Analytics

Finding all occurrence of Pattern:

re.findall(): This function returns all non-overlapping matches of pattern in string in the form of a list of strings. The function scans the string from left to right. The matches are returned in the order in which they are found.

Regular expressions is vast. It is a complete library in itself. It can do a lot of stuff. The below example is a way of doing powerful things. We can extract email address from the text. We can make our own Web crawler and scrappers in Python.

To learn more about regular expression in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.