Searching an Occurrence of Pattern |
re.search() method either returns None (if the pattern doesn't match)
or a re.MatchObject re.MatchObject contains information about the matching part of the string method stops after the first match best suited for testing a regular expression more than extracting data import re # regular expression to match a date string in the form of Month name followed by day number 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())) # "June 24" print ("Full match: %s" % (match.group(0))) # "June" print ("Month: %s" % (match.group(1))) # "24" 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() function attempts to match pattern to whole string function returns a match object on success, None on failure syntax 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 (|).example # A Python program to demonstrate re.match(). import re # a sample function that uses regular expressions to find month and day of a date. 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() returns all non-overlapping matches of pattern as a list of strings the string is scanned left-to-right matches are returned in the order found example import re string = "Hello my Number is 123456789 and my friend's number is 987654321" # find digits. regex = '\d+' match = re.findall(regex, string) print(match)output ['123456789', '987654321'] |