Regular Expression in Python with Examples | Set 1

08 Jun, 2022
A Regular Expressions (RegEx) is a special sequence of characters that uses a search pattern to find a string or set of strings. It can detect the presence or absence of a text by matching it with a particular pattern, and also can split a pattern into one or more sub-patterns. Python provides a re module that supports the use of regex in Python. Its primary function is to offer a search, where it takes a regular expression and a string. Here, it either returns the first match or else none.
Example:
Python3
import re s = 'GeeksforGeeks: A computer science portal for geeks' match = re.search(r'portal', s) print('Start Index:', match.start())print('End Index:', match.end())
Start Index: 34
End Index: 40
The above code gives the starting index and the ending index of the string portal. 
Note: Here r character (r’portal’) stands for raw, not regex. The raw string is slightly different from a regular string, it won’t interpret the \ character as an escape character. This is because the regular expression engine uses \ character for its own escaping purpose.
Before starting with the Python regex module let’s see how to actually write regex using metacharacters or special sequences. 
To understand the RE analogy, MetaCharacters are useful, important, and will be used in functions of module re. Below is the list of metacharacters.
Let’s discuss each of these metacharacters in detail 
The backslash (\) makes sure that the character is not treated in a special way. This can be considered a way of escaping metacharacters. For example, if you want to search for the dot(.) in the string then you will find that dot(.) will be treated as a special character as is one of the metacharacters (as shown in the above table). So for this case, we will use the backslash(\) just before the dot(.) so that it will lose its specialty. See the below example for a better understanding.
Example: 
Python3
import re s = 'geeks.forgeeks' # without using \match = re.search(r'.', s)print(match) # using \match = re.search(r'\.', s)print(match)
<_sre.SRE_Match object; span=(0, 1), match='g'>
<_sre.SRE_Match object; span=(5, 6), match='.'>
Square Brackets ([]) represent a character class consisting of a set of characters that we wish to match. For example, the character class [abc] will match any single a, b, or c. 
We can also specify a range of characters using – inside the square brackets. For example, 
[0, 3] is sample as [0123]
[a-c] is same as [abc]
We can also invert the character class using the caret(^) symbol. For example, 
[^0-3] means any number except 0, 1, 2, or 3
[^a-c] means any character except a, b, or c
Caret (^) symbol matches the beginning of the string i.e. checks whether the string starts with the given character(s) or not. For example –  
^g will check if the string starts with g such as geeks, globe, girl, g, etc.
^ge will check if the string starts with ge such as geeks, geeksforgeeks, etc.
Dollar($) symbol matches the end of the string i.e checks whether the string ends with the given character(s) or not. For example – 
s$ will check for the string that ends with a such as geeks, ends, s, etc.
ks$ will check for the string that ends with ks such as geeks, geeksforgeeks, ks, etc.
Dot(.) symbol matches only a single character except for the newline character (\n). For example –  
a.b will check for the string that contains any character at the place of the dot such as acb, acbd, abbb, etc
.. will check if the string contains at least 2 characters
Or symbol works as the or operator meaning it checks whether the pattern before or after the or symbol is present in the string or not. For example –  
a|b will match any string that contains a or b such as acd, bcd, abcd, etc.
Question mark(?) checks if the string before the question mark in the regex occurs at least once or not at all. For example –  
ab?c will be matched for the string ac, acb, dabc but will not be matched for abbc because there are two b. Similarly, it will not be matched for abdc because b is not followed by c.
Star (*) symbol matches zero or more occurrences of the regex preceding the * symbol. For example –  
ab*c will be matched for the string ac, abc, abbbc, dabc, etc. but will not be matched for abdc because b is not followed by c.
Plus (+) symbol matches one or more occurrences of the regex preceding the + symbol. For example –  
ab+c will be matched for the string abc, abbc, dabc, but will not be matched for ac, abdc because there is no b in ac and b is not followed by c in abdc.
Braces match any repetitions preceding regex from m to n both inclusive. For example –  
a{2, 4} will be matched for the string aaab, baaaac, gaad, but will not be matched for strings like abc, bc because there is only one a or no a in both the cases.
Group symbol is used to group sub-patterns. For example –  
(a|b)cd will match for strings like acd, abcd, gacd, etc.
Special sequences do not match for the actual character in the string instead it tells the specific location in the search string where the match must occur. It makes it easier to write commonly used patterns.  
Python has a module named re that is used for regular expressions in Python. We can import this module by using the import statement.
Example: Importing re module in Python 
Python3
import re
Let’s see various functions provided by this module to work with regex in Python. 
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.
Example: Finding all occurrences of a pattern 
Python3
# A Python program to demonstrate working of# findall()import re # A sample text string where regular expression# is searched.string = """Hello my Number is 123456789 and            my friend's number is 987654321""" # A sample regular expression to find digits.regex = '\d+' match = re.findall(regex, string)print(match) # This example is contributed by Ayush Saluja.
['123456789', '987654321']
Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. 
Example 1:
Python
# Module Regular Expression is imported# using __import__().import re # compile() creates regular expression# character class [a-e],# which is equivalent to [abcde].# class [abcde] will match with string with# 'a', 'b', 'c', 'd', 'e'.p = re.compile('[a-e]') # findall() searches for the Regular Expression# and return a list upon findingprint(p.findall("Aye, said Mr. Gibenson Stark"))
Output: 
['e', 'a', 'd', 'b', 'e', 'a']
Understanding the Output: 
First occurrence is ‘e’ in “Aye” and not ‘A’, as it is Case Sensitive.
Next Occurrence is ‘a’ in “said”, then ‘d’ in “said”, followed by ‘b’ and ‘e’ in “Gibenson”, the Last ‘a’ matches with “Stark”.
Metacharacter backslash ‘\’ has a very important role as it signals various sequences. If the backslash is to be used without its special meaning as metacharacter, use’\\’
Example 2: Set class [\s,.] will match any whitespace character,  ‘,’,  or, ‘.’ . 
Python
import re # \d is equivalent to [0-9].p = re.compile('\d')print(p.findall("I went to him at 11 A.M. on 4th July 1886")) # \d+ will match a group on [0-9], group# of one or greater sizep = re.compile('\d+')print(p.findall("I went to him at 11 A.M. on 4th July 1886"))
Output: 
['1', '1', '4', '1', '8', '8', '6']
['11', '4', '1886']
Example 3:
Python
import re # \w is equivalent to [a-zA-Z0-9_].p = re.compile('\w')print(p.findall("He said * in some_lang.")) # \w+ matches to group of alphanumeric character.p = re.compile('\w+')print(p.findall("I went to him at 11 A.M., he \said *** in some_language.")) # \W matches to non alphanumeric characters.p = re.compile('\W')print(p.findall("he said *** in some_language."))
Output: 
['H', 'e', 's', 'a', 'i', 'd', 'i', 'n', 's', 'o', 'm', 'e', '_', 'l', 'a', 'n', 'g']
['I', 'went', 'to', 'him', 'at', '11', 'A', 'M', 'he', 'said', 'in', 'some_language']
[' ', ' ', '*', '*', '*', ' ', ' ', '.']
Example 4:
Python
import re # '*' replaces the no. of occurrence# of a character.p = re.compile('ab*')print(p.findall("ababbaabbb"))
Output: 
['ab', 'abb', 'a', 'abbb']
Understanding the Output: 
Our RE is ab*, which ‘a’ accompanied by any no. of ‘b’s, starting from 0.
Output ‘ab’, is valid because of single ‘a’ accompanied by single ‘b’.
Output ‘abb’, is valid because of single ‘a’ accompanied by 2 ‘b’.
Output ‘a’, is valid because of single ‘a’ accompanied by 0 ‘b’.
Output ‘abbb’, is valid because of single ‘a’ accompanied by 3 ‘b’.
Split string by the occurrences of a character or a pattern, upon finding that pattern, the remaining characters from the string are returned as part of the resulting list. 
Syntax : 
re.split(pattern, string, maxsplit=0, flags=0)
The First parameter, pattern denotes the regular expression, string is the given string in which pattern will be searched for and in which splitting occurs, maxsplit if not provided is considered to be zero ‘0’, and if any nonzero value is provided, then at most that many splits occur. If maxsplit = 1, then the string will split once only, resulting in a list of length 2. The flags are very useful and can help to shorten code, they are not necessary parameters, eg: flags = re.IGNORECASE, in this split, the case, i.e. the lowercase or the uppercase will be ignored.
Example 1:
Python
from re import split # '\W+' denotes Non-Alphanumeric Characters# or group of characters Upon finding ','# or whitespace ' ', the split(), splits the# string from that pointprint(split('\W+', 'Words, words , Words'))print(split('\W+', "Word's words Words")) # Here ':', ' ' ,',' are not AlphaNumeric thus,# the point where splitting occursprint(split('\W+', 'On 12th Jan 2016, at 11:02 AM')) # '\d+' denotes Numeric Characters or group of# characters Splitting occurs at '12', '2016',# '11', '02' onlyprint(split('\d+', 'On 12th Jan 2016, at 11:02 AM'))
Output: 
['Words', 'words', 'Words']
['Word', 's', 'words', 'Words']
['On', '12th', 'Jan', '2016', 'at', '11', '02', 'AM']
['On ', 'th Jan ', ', at ', ':', ' AM']
Example 2:
Python
import re # Splitting will occurs only once, at# '12', returned list will have length 2print(re.split('\d+', 'On 12th Jan 2016, at 11:02 AM', 1)) # 'Boy' and 'boy' will be treated same when# flags = re.IGNORECASEprint(re.split('[a-f]+', 'Aey, Boy oh boy, come here', flags=re.IGNORECASE))print(re.split('[a-f]+', 'Aey, Boy oh boy, come here'))
Output: 
['On ', 'th Jan 2016, at 11:02 AM']
['', 'y, ', 'oy oh ', 'oy, ', 'om', ' h', 'r', '']
['A', 'y, Boy oh ', 'oy, ', 'om', ' h', 'r', '']
The ‘sub’ in the function stands for SubString, a certain regular expression pattern is searched in the given string(3rd parameter), and upon finding the substring pattern is replaced by repl(2nd parameter), count checks and maintains the number of times this occurs. 
Syntax:
 re.sub(pattern, repl, string, count=0, flags=0)
Example 1:
Python
import re # Regular Expression pattern 'ub' matches the# string at "Subject" and "Uber". As the CASE# has been ignored, using Flag, 'ub' should# match twice with the string Upon matching,# 'ub' is replaced by '~*' in "Subject", and# in "Uber", 'Ub' is replaced.print(re.sub('ub', '~*', 'Subject has Uber booked already',             flags=re.IGNORECASE)) # Consider the Case Sensitivity, 'Ub' in# "Uber", will not be replaced.print(re.sub('ub', '~*', 'Subject has Uber booked already')) # As count has been given value 1, the maximum# times replacement occurs is 1print(re.sub('ub', '~*', 'Subject has Uber booked already',             count=1, flags=re.IGNORECASE)) # 'r' before the pattern denotes RE, \s is for# start and end of a String.print(re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam',             flags=re.IGNORECASE))
Output 
S~*ject has ~*er booked already
S~*ject has Uber booked already
S~*ject has Uber booked already
Baked Beans & Spam
subn() is similar to sub() in all ways, except in its way of providing output. It returns a tuple with a count of the total of replacement and the new string rather than just the string. 
Syntax:
 re.subn(pattern, repl, string, count=0, flags=0)
Example:
Python
import re print(re.subn('ub', '~*', 'Subject has Uber booked already')) t = re.subn('ub', '~*', 'Subject has Uber booked already',            flags=re.IGNORECASE)print(t)print(len(t)) # This will give same output as sub() would haveprint(t[0])
Output 
('S~*ject has Uber booked already', 1)
('S~*ject has ~*er booked already', 2)
Length of Tuple is:  2
S~*ject has ~*er booked already
Returns string with all non-alphanumerics backslashed, this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
Syntax:
re.escape(string)
Example:
Python
import re # escape() returns a string with BackSlash '\',# before every Non-Alphanumeric Character# In 1st case only ' ', is not alphanumeric# In 2nd case, ' ', caret '^', '-', '[]', '\'# are not alphanumericprint(re.escape("This is Awesome even 1 AM"))print(re.escape("I Asked what is this [a-9], he said \t ^WoW"))
This\ is\ Awesome\ even\ 1\ AM
I\ Asked\ what\ is\ this\ \[a\-9\]\,\ he\ said\ \    \ \^WoW
This method either returns None (if the pattern doesn’t match), or a re.MatchObject 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.
Example: Searching for an occurrence of the pattern
Python3
# A Python program to demonstrate working of re.match().import re # Lets use a regular expression to match a date string# in the form of Month name followed by day numberregex = r"([a-zA-Z]+) (\d+)" match = re.search(regex, "I was born on June 24") if match != None:     # We reach here when the expression "([a-zA-Z]+) (\d+)"    # matches the date string.     # This will print [14, 21), since it matches at index 14    # and ends at 21.    print ("Match at index %s, %s" % (match.start(), match.end()))     # We us group() method to get all the matches and    # captured groups. The groups contain the matched values.    # In particular:    # match.group(0) always returns the fully matched string    # match.group(1) match.group(2), ... return the capture    # groups in order from left to right in the input string    # match.group() is equivalent to match.group(0)     # So this will print "June 24"    print ("Full match: %s" % (match.group(0)))     # So this will print "June"    print ("Month: %s" % (match.group(1)))     # So this will print "24"    print ("Day: %s" % (match.group(2))) else:    print ("The regex pattern does not match.")
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
A Match object contains all the information about the search and the result and if there is no match found then None will be returned. Let’s see some of the commonly used methods and attributes of the match object.
match.re attribute returns the regular expression passed and match.string attribute returns the string passed.
Example: Getting the string and the regex of the matched object
Python3
import re s = "Welcome to GeeksForGeeks" # here x is the match objectres = re.search(r"\bG", s) print(res.re)print(res.string)
re.compile('\\bG')
Welcome to GeeksForGeeks
start() method returns the starting index of the matched substring
end() method returns the ending index of the matched substring
span() method returns a tuple containing the starting and the ending index of the matched substring
Example: Getting index of matched object 
Python3
import re s = "Welcome to GeeksForGeeks" # here x is the match objectres = re.search(r"\bGee", s) print(res.start())print(res.end())print(res.span())
11
14
(11, 14)
group() method returns the part of the string for which the patterns match. See the below example for a better understanding.
Example: Getting matched substring 
Python3
import re s = "Welcome to GeeksForGeeks" # here x is the match objectres = re.search(r"\D{2} t", s) print(res.group())
me t
In the above example, our pattern specifies for the string that contains at least 2 characters which are followed by a space, and that space is followed by a t. 
Python Programming Tutorial | Python Modules - Part 2 (Regular Expression) | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersPython Programming Tutorial | Python Modules - Part 2 (Regular Expression) | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:30•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=JKNLy55G2z0" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Related Article : https://www.geeksforgeeks.org/regular-expressions-python-set-1-search-match-find/
Reference: https://docs.python.org/2/library/re.html
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or 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. 
nidhi_biet
ManasChhabra2
shubham_singh
chaudhary_19
manasdutta
nikhilaggarwal3
ruhelaa48
sagar0719kumar
kapoorsagar226
muvvalaaravind
gautamverma01gv
simmytarika5
arshadali02177
kushagrathegame1
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.