Python RegEx | Regular Expression Tutorial with Examples

Python RegEx | Regular Expression Tutorial with Examples

The RegEx of the Regular Expression is actually a sequene of charaters that is used for searching or pattern matching.

Python has module re for matching search patterns. It comes inbuilt with Python installation. You just need to import and use it.

Let’s see this simple Python regex example.

Search if there is at least two consecutive English uppercase letters.

import re

print(bool(re.search("[A-Z]{2}", "cdABp")))
#True

print(bool(re.search("[A-Z]{2}", "1Z")))
#False

print(bool(re.search("[A-Z]{2}", "A1P")))
#False

RegEx Functions

Python re modules offer the following functions for pattern matching. You can use them for different purposes.

Function Description
search returns match object if match found
findall returns list of all the matches from the given string
split returns list of string where it has been split for each match
sub returns string by replacing all the matches

RegEx Sets

Just like the English uppercase letters example, we have seen above, there are other different sets you can use in Python regex.

Set Matching Characters
[A-Z] set which returns a match for any character between ‘A’ to ‘Z’ (uppercase letters)
[a-z] set which returns a match for any character between ‘a’ to ‘z’ (lowercase letters)
[0-9] set which returns a match for any character between ‘0’ to ‘9’ (numeric number)
[A-Za-z] set which returns a match for any alphabetic character (includes both uppercase and lowercase letters)
[A-Za-z0-9] set which returns a match for any alphabetic or numeric character (includes both uppercase, lowercase letters and numbers)
[apx] set which returns match if one of the character (‘a’, ‘p’, ‘x’) are present

Special RegEx Characters

Character Description Example
[] to define set of characters [A-Z]
match all the uppercase letters
{} to match the exact number of characters {2}
match only two characters
^ to check the prefix (starts with) “^CSE”
string start with “CSE”
$ to check the postfix (ends with) “stack$”
string ends with “stack”
* to match zero or more occurrences pos*
match the string contains “po” followed by zero or more “s” characters
+ to match one or more occurrences pos+
match the string contains “po” followed by one or more “s” characters
| or operation “CSE|stack”
match string contains either “CSE” or “stack”:
() form a group
. to match any character (except newline character) “CS..ack”
match the string containing “CS” followed by any other two characters and then “ack”
\ to match special sequence “\d”
match all digit characters

Python RegEx Coding Examples

1. search() function

Search the first occurrence of “ing” in the given string.

import re
match_obj = re.search("ing", "CSEstack Programming Portal")
print(match_obj.start())
print(match_obj.end())

Output

17
20

Explanation

  • The search function returns a match object.
  • If no match found, it returns the NoneType object.
  • You can use the start() and end() methods of match object to get the start and end index of the matching pattern.
  • You can also convert a match object into the True/False Boolean values using bool() method. Return True if match found, else False.

2. findall() function

Find all the matching which contains character “r” and followed by two any other characters.

import re
match_list = re.findall("r..", "Programming Portal- Keep Coding")
print(match_list)

Output

['rog', 'ram', 'rta']

Explanation

  • Method findall() returns Python list object. So, you can perform any operations on matches that we do with the Python list.

3. split() function

Split the given string by occurrence of character ‘a’ or ‘g’.

import re
match_list = re.split("a|g", "CSEstack Programming Portal")
print(match_list)

Output

['CSEst', 'ck Pro', 'r', 'mmin', ' Port', 'l']

Explanation

  • Like findall(), split() method also returns Python list.

4. sub() function

Replace all the occurrences of substring “cse” with “CSE”

import re
match_string = re.sub("cse", "CSE", "Learn coding from csestack.")
print(match_string)

Output

Learn programming from CSEstack.

Explanation

  • Method sub(), returns the Python string.

These are the simple examples to demonstarte different reguylar explression and methods.

Regular expressions are extremely useful in different fields like data analytics or projects for pattern matching.

Python RegEx Questions asked in Interview

Here are some of the examples asked in coding interviews. Take them as coding exercise and practice solving them.

This is all about Regular Expression in Python (Python RegEx). Hope you find this tutorial useful for your learning.

Leave a Reply

Your email address will not be published. Required fields are marked *