Regular Expression in Python with Examples | Set 1
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.
Python3
import re
regex = r "([a-zA-Z]+) (\d+)"
match = re.search(regex, "I was born on June 24" )
if match ! = None :
print ( "Match at index %s, %s" % (match.start(), match.end()))
print ( "Full match: %s" % (match.group( 0 )))
print ( "Month: %s" % (match.group( 1 )))
print ( "Day: %s" % (match.group( 2 )))
else :
print ( "The regex pattern does not match." )
|
Output :
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
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.
re.match(pattern, string, flags=0)
pattern : Regular expression to be matched.
string : String where pattern is searched
flags : We can specify different flags
using bitwise OR (|).
Python3
import re
def findMonthAndDate(string):
regex = r "([a-zA-Z]+) (\d+)"
match = re.match(regex, string)
if match = = None :
print ( "Not a valid date" )
return
print ( "Given Data: %s" % (match.group()))
print ( "Month: %s" % (match.group( 1 )))
print ( "Day: %s" % (match.group( 2 )))
findMonthAndDate( "Jun 24" )
print ("")
findMonthAndDate( "I was born on June 24" )
|
Output:
Given Data: Jun 24
Month: Jun
Day: 24
Not a valid date
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).
Python3
import re
string =
regex = '\d+'
match = re.findall(regex, string)
print (match)
|
Output :
['123456789', '987654321']
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 the 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.
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. 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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Dec, 2021
Like Article
Save Article