The module re provides support for regular expressions in Python. Below are main methods in this module.
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.
Match at index 14, 21 Full match: June 24 Month: June Day: 24
re.match() : This function attempts to match pattern to whole string. The re.match function returns a match object on success, None on failure.
re.match(pattern, string, flags=0) pattern : Regular expression to be matched. string : String where p attern is searched flags : We can specify different flags using bitwise OR (|).
Given Data: Jun 24 Month: Jun Day: 24 Not a valid date
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).
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.
# extract all email addresses and add them into the resulting set new_emails = set(re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", text, re.I))
We will soon be discussing more methods on regular expressions.
This article is contributed by Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Regular Expressions in Python
- Python Flags to Tune the Behavior of Regular Expressions
- Extracting email addresses using regular expressions in Python
- Python | re.search() vs re.match()
- Python - Test rear digit match in all list elements
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Python | Get match indices
- Python | Pandas Series.str.match()
- Python | Substring Key match in dictionary
- Python | Prefix key match in dictionary
- Python - Match Kth number digit in list elements
- Python - Index match element Product
- Python - Retain K match index values from other list
- Python - Number of positions where Substrings Match of Length K
- Find all the numbers in a string using regular expression in Python
- Mahotas - Match template to image exactly
- Overuse of lambda expressions in Python
- Python List Comprehensions vs Generator Expressions
- Python | Generate Personalized Data from given list of expressions
- Plot Mathematical Expressions in Python using Matplotlib