Open In App

Regular Expression (RegEx) in Python with Examples

A Regular Expression or 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.



Regex Module in Python

Python has a built-in 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 






# importing re module
import re

How to Use RegEx in Python?

You can use RegEx in Python after importing re module.

Example:

This Python code uses regular expressions to search for the word “portal” in the given string and then prints the start and end indices of the matched word within the string.




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())

Output
Start Index: 34
End Index: 40





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. 

Metacharacters

Metacharacters are the characters with special meaning.

To understand the RE analogy, Metacharacters are useful and important. They will be used in functions of module re. Below is the list of metacharacters.

MetaCharacters

Description

\

Used to drop the special meaning of character following it

[]

Represent a character class

^

Matches the beginning

$

Matches the end

.

Matches any character except newline

|

Means OR (Matches with any of the characters separated by it.

?

Matches zero or one occurrence

*

Any number of occurrences (including 0 occurrences)

+

One or more occurrences

{}

Indicate the number of occurrences of a preceding regex to match.

()

Enclose a group of Regex

Let’s discuss each of these metacharacters in detail:

1. \ – Backslash

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: 

The first search (re.search(r'.', s)) matches any character, not just the period, while the second search (re.search(r'\.', s)) specifically looks for and matches the period character.




import re
  
s = 'geeks.forgeeks'
  
# without using \
match = re.search(r'.', s)
print(match)
  
# using \
match = re.search(r'\.', s)
print(match)

Output
<re.Match object; span=(0, 1), match='g'>
<re.Match object; span=(5, 6), match='.'>





2. [] – Square Brackets

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, 

We can also invert the character class using the caret(^) symbol. For example, 

Example:

In this code, you’re using regular expressions to find all the characters in the string that fall within the range of ‘a’ to ‘m’. The re.findall() function returns a list of all such characters. In the given string, the characters that match this pattern are: ‘c’, ‘k’, ‘b’, ‘f’, ‘j’, ‘e’, ‘h’, ‘l’, ‘d’, ‘g’.




import re
  
string = "The quick brown fox jumps over the lazy dog"
pattern = "[a-m]"
result = re.findall(pattern, string)
  
print(result)

Output
['h', 'e', 'i', 'c', 'k', 'b', 'f', 'j', 'm', 'e', 'h', 'e', 'l', 'a', 'd', 'g']





3. ^ – Caret

Caret (^) symbol matches the beginning of the string i.e. checks whether the string starts with the given character(s) or not. For example –  

Example:

This code uses regular expressions to check if a list of strings starts with “The”. If a string begins with “The,” it’s marked as “Matched” otherwise, it’s labeled as “Not matched”.




import re
regex = r'^The'
strings = ['The quick brown fox', 'The lazy dog', 'A quick brown fox']
for string in strings:
    if re.match(regex, string):
        print(f'Matched: {string}')
    else:
        print(f'Not matched: {string}')

Output
Matched: The quick brown fox
Matched: The lazy dog
Not matched: A quick brown fox





4. $ – Dollar

Dollar($) symbol matches the end of the string i.e checks whether the string ends with the given character(s) or not. For example-

Example:

This code uses a regular expression to check if the string ends with “World!”. If a match is found, it prints “Match found!” otherwise, it prints “Match not found”.




import re
  
string = "Hello World!"
pattern = r"World!$"
  
match = re.search(pattern, string)
if match:
    print("Match found!")
else:
    print("Match not found.")

Output
Match found!





5. . – Dot

Dot(.) symbol matches only a single character except for the newline character (\n). For example –  

Example:

This code uses a regular expression to search for the pattern “brown.fox” within the string. The dot (.) in the pattern represents any character. If a match is found, it prints “Match found!” otherwise, it prints “Match not found”.




import re
  
string = "The quick brown fox jumps over the lazy dog."
pattern = r"brown.fox"
  
match = re.search(pattern, string)
if match:
    print("Match found!")
else:
    print("Match not found.")

Output
Match found!





6. | – Or

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 –  

7. ? – Question Mark

The question mark (?) is a quantifier in regular expressions that indicates that the preceding element should be matched zero or one time. It allows you to specify that the element is optional, meaning it may occur once or not at all. For example,

8.* – Star

Star (*) symbol matches zero or more occurrences of the regex preceding the * symbol. For example –  

9. + – Plus

Plus (+) symbol matches one or more occurrences of the regex preceding the + symbol. For example –  

10. {m, n} – Braces

Braces match any repetitions preceding regex from m to n both inclusive. For example –  

11. (<regex>) – Group

Group symbol is used to group sub-patterns. For example –  

Special Sequences

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.  

List of special sequences 

Special Sequence

Description

Examples

\A

Matches if the string begins with the given character

\Afor 

for geeks

for the world

\b

Matches if the word begins or ends with the given character. \b(string) will check for the beginning of the word and (string)\b will check for the ending of the word.

\bge

geeks

get

\B

It is the opposite of the \b i.e. the string should not start or end with the given regex.

\Bge

together

forge

\d

Matches any decimal digit, this is equivalent to the set class [0-9]

\d

123

gee1

\D

Matches any non-digit character, this is equivalent to the set class [^0-9]

\D

geeks

geek1

\s

Matches any whitespace character.

\s

gee ks

a bc a

\S

Matches any non-whitespace character

\S

a bd

abcd

\w

Matches any alphanumeric character, this is equivalent to the class [a-zA-Z0-9_].

\w

123

geeKs4

\W

Matches any non-alphanumeric character.

\W

>$

gee<>

\Z

Matches if the string ends with the given regex

ab\Z

abcdab

abababab

RegEx Functions

re module contains many functions that help us to search a string for a match.

Let’s see various functions provided by this module to work with regex in Python. 

Function Description
re.findall() finds and returns all matching occurrences in a list
re.compile()  Regular expressions are compiled into pattern objects
re.split()  Split string by the occurrences of a character or a pattern.
re.sub()  Replaces all occurrences of a character or patter with a replacement string.
re.escape() Escapes special character
re.search() Searches for first occurrence of character or pattern

Let’s see the working of these RegEx functions with definition and examples:

1. 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.

Finding all occurrences of a pattern 

This code uses a regular expression (\d+) to find all the sequences of one or more digits in the given string. It searches for numeric values and stores them in a list. In this example, it finds and prints the numbers “123456789” and “987654321” from the input string.




import re
string = """Hello my Number is 123456789 and
            my friend's number is 987654321"""
regex = '\d+'
  
match = re.findall(regex, string)
print(match)

Output
['123456789', '987654321']





2. re.compile() 

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:

The code uses a regular expression pattern [a-e] to find and list all lowercase letters from ‘a’ to ‘e’ in the input string “Aye, said Mr. Gibenson Stark”. The output will be ['e', 'a', 'd', 'b', 'e'], which are the matching characters.




import re
p = re.compile('[a-e]')
  
print(p.findall("Aye, said Mr. Gibenson Stark"))

Output
['e', 'a', 'd', 'b', 'e', 'a']





Understanding the Output: 

Example 2: Set class [\s,.] will match any whitespace character,  ‘,’,  or, ‘.’ . 

The code uses regular expressions to find and list all single digits and sequences of digits in the given input strings. It finds single digits with \d and sequences of digits with \d+.




import re
p = re.compile('\d')
print(p.findall("I went to him at 11 A.M. on 4th July 1886"))
  
p = 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:

The code uses regular expressions to find and list word characters, sequences of word characters, and non-word characters in input strings. It provides lists of the matched characters or sequences.




import re
  
p = re.compile('\w')
print(p.findall("He said * in some_lang."))
  
p = re.compile('\w+')
print(p.findall("I went to him at 11 A.M., he \
said *** in some_language."))
  
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:

The code uses a regular expression pattern ‘ab*’ to find and list all occurrences of ‘ab’ followed by zero or more ‘b’ characters in the input string “ababbaabbb”. It returns the following list of matches: [‘ab’, ‘abb’, ‘abbb’].




import re
p = re.compile('ab*')
print(p.findall("ababbaabbb"))

Output
['ab', 'abb', 'a', 'abbb']





Understanding the Output: 

3. re.split() 

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:

Splits a string using non-word characters and spaces as delimiters, returning words: ['Words', 'words', 'Words']. Considers apostrophes as non-word characters: ['Word', 's', 'words', 'Words']. Splits using non-word characters and digits: ['On', '12th', 'Jan', '2016', 'at', '11', '02', 'AM']. Splits using digits as the delimiter: ['On ', 'th Jan ', ', at ', ':', ' AM'].




from re import split
  
print(split('\W+', 'Words, words , Words'))
print(split('\W+', "Word's words Words"))
print(split('\W+', 'On 12th Jan 2016, at 11:02 AM'))
print(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:

First statement splits the string at the first occurrence of one or more digits: ['On ', 'th Jan 2016, at 11:02 AM']. second splits the string using lowercase letters a to f as delimiters, case-insensitive: ['', 'y, ', 'oy oh ', 'oy, ', 'ome here']. Third splits the string using lowercase letters a to f as delimiters, case-sensitive: ['', 'ey, Boy oh ', 'oy, ', 'ome here'].




import re
print(re.split('\d+', 'On 12th Jan 2016, at 11:02 AM', 1))
print(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', '']





4. re.sub() 

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:




import re
print(re.sub('ub', '~*', 'Subject has Uber booked already'
             flags=re.IGNORECASE))
print(re.sub('ub', '~*', 'Subject has Uber booked already'))
print(re.sub('ub', '~*', 'Subject has Uber booked already',
             count=1, flags=re.IGNORECASE))
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





5. re.subn() 

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:

re.subn() replaces all occurrences of a pattern in a string and returns a tuple with the modified string and the count of substitutions made. It’s useful for both case-sensitive and case-insensitive substitutions.




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))
print(t[0])

Output
('S~*ject has Uber booked already', 1)
('S~*ject has ~*er booked already', 2)
2
S~*ject has ~*er booked already



6. re.escape()

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:

re.escape() is used to escape special characters in a string, making it safe to be used as a pattern in regular expressions. It ensures that any characters with special meanings in regular expressions are treated as literal characters.




import re
print(re.escape("This is Awesome even 1 AM"))
print(re.escape("I Asked what is this [a-9], he said \t ^WoW"))

Output
This\ is\ Awesome\ even\ 1\ AM
I\ Asked\ what\ is\ this\ \[a\-9\]\,\ he\ said\ \    \ \^WoW



7. re.search()

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

This code uses a regular expression to search for a pattern in the given string. If a match is found, it extracts and prints the matched portions of the string.

In this specific example, it searches for a pattern that consists of a month (letters) followed by a day (digits) in the input string “I was born on June 24”. If a match is found, it prints the full match, the month, and the day.




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



SETS

A Set is a set of characters enclosed in ‘[]’ brackets. Sets are used to match a single character in the set of characters specified between brackets. Below is the list of Sets:

Set Description
\{n,\} Quantifies the preceding character or group and matches at least n occurrences.
* Quantifies the preceding character or group and matches zero or more occurrences.
[0123] Matches the specified digits (0, 1, 2, or 3)
[^arn] matches for any character EXCEPT a, r, and n
\d Matches any digit (0-9).
[0-5][0-9] matches for any two-digit numbers from 00 and 59
\w Matches any alphanumeric character (a-z, A-Z, 0-9, or _).
[a-n] Matches any lower case alphabet between a and n.
\D Matches any non-digit character.
[arn] matches where one of the specified characters (a, r, or n) are present
[a-zA-Z] matches any character between a and z, lower case OR upper case
[0-9] matches any digit between 0 and 9

Match Object

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.

1. Getting the string and the regex

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

The code searches for the letter “G” at a word boundary in the string “Welcome to GeeksForGeeks” and prints the regular expression pattern (res.re) and the original string (res.string).




import re
s = "Welcome to GeeksForGeeks"
res = re.search(r"\bG", s)
  
print(res.re)
print(res.string)

Output
re.compile('\\bG')
Welcome to GeeksForGeeks





2. Getting index of matched object

Example: Getting index of matched object 

The code searches for the substring “Gee” at a word boundary in the string “Welcome to GeeksForGeeks” and prints the start index of the match (res.start()), the end index of the match (res.end()), and the span of the match (res.span()).




import re
  
s = "Welcome to GeeksForGeeks"
  
res = re.search(r"\bGee", s)
  
print(res.start())
print(res.end())
print(res.span())

Output
11
14
(11, 14)





3. Getting matched substring

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 

The code searches for a sequence of two non-digit characters followed by a space and the letter ‘t’ in the string “Welcome to GeeksForGeeks” and prints the matched text using res.group().




import re
s = "Welcome to GeeksForGeeks"
res = re.search(r"\D{2} t", s)
print(res.group())

Output
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. 

Related Article : 
https://www.geeksforgeeks.org/regular-expressions-python-set-1-search-match-find/amp/

Reference: 
https://docs.python.org/2/library/re.html

We have discussed RegEx in Python, we have gone through the metacharacters, functions and special sequences in RegEx Python.

Regular Expression is a very important concept in Python, we have tried to explain it in a easy way. Hope it helps in your Python Journey!! 


Article Tags :