The module re provides support for regular expressions in Python. Below are main methods in this module.
Searching an occurrence of pattern
re.search() : This method either returns None (if the pattern doesn’t match), or a re.MatchObject that contains information about the matching part of the string. This method stops after the first match, so this is best suited for testing a regular expression more than extracting data.
python - Sample - python code :
python tutorial - Output :
Matching a Pattern with Text
re.match() : This function attempts to match pattern to whole string. The re.match function returns a match object on success, None on failure.
python - Sample - python code :
python - Sample - python code :
Finding all occurrences of a pattern
re.findall() : Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found (Source : Python Docs).
python - Sample - python code :
python tutorial - Output :
Regular expression is a vast topic. It’s a complete library. Regular expressions can do a lot of stuff. You can Match, Search, Replace, Extract a lot of data. For example, below small code is so powerful that it can extract email address from a text. So we can make our own Web Crawlers and scrappers in python with easy.Look at below regex.