REGULAR EXPRESSION IN PYTHON
Regular expression (RegEx) is a search pattern (a sequence of characters) that is used to validate if a string follows a specific pattern. The python module for handling regular expressions is re, it is imported this way:
import re
Python Regex Metacharacters
Metacharacters are characters that have a special meaning. They include:
1. []: For a set of characters. A set is a group of characters inside square brackets [] with a special meaning. Examples:
- [Hod]: Returns all characters in the string that equals character ‘H’, ‘o’ or ‘d’. e.g.
import re
text = “Hello World!”
search = re.findall(“[Hod]”, text)
print(search)
The above outputs the list [‘H’, ‘o’, ‘o’, ‘d’] but if none of the characters was found an empty list is returned.
- [a-n]: Returns all characters in the string that are members of ‘a’ to ’n’. e.g.
import re
text = “Hello World”
search = re.findall(“[a-n]”, text)
print(search)
The above outputs the list [‘e’, ‘l’, ‘l’, ‘l’, ‘d’].
- [^Hod]: Returns all characters (including whitespaces) in the string that are not ‘H’, ‘o’ or ‘d’. e.g.
import re
text = “Hello World!”
search = re.findall(“[^Hod]”, text)
print(search)
This outputs the list [‘e’, ‘l’, ‘l’, ‘ ‘, ‘W’, ‘r’, ‘l’, ‘!’].
- [0123]: It returns all characters in the string that are digit 0,1,2,or 3. E.g.
import re
text = “Hello World2!”
search = re.findall(“[0123]”, text)
print(search)
This outputs the list [‘2’], which the only numeric character within 0,1,2,3 that is part of the string.
- [0–9]: Returns all characters in the string that are members of 0–9. E.g.
import re
text = “Hello World Number 92!”
search = re.findall(“[0–9]”, text)
print(search)
The above outputs the list [‘9’, ‘2’].
- [a-zA-Z]: Returns all characters in the string that are members of lowercase a-z and uppercase A-Z. e.g.
import re
text = “Hello”
search = re.findall(“[a-zA-Z]”, text)
print(search)
Above outputs the list [‘H’, ‘e’, ‘l’, ‘l’, ‘o’]
- [+]: Returns all characters in the string that are the symbol +. Here it has no special meaning likewise all other characters like *,.,|,(,),&,{,} when used in sets e.g.
import re
text = “y = ax + n”
search = re.findall(“[+=]”, text)
print(search)
Above outputs the list [‘=’, ‘+’]
2. \: It is used to escape or signal a special sequence. The special sequence we have include: \A, \b, \B, \d, \D, \s, \S, \w, \W, \Z.
- \A: Returns sequence of characters searched for if it appeared at the beginning of the string the regex is applied on. E.g.
import re
text = “It is raining”
search = re.findall(“\AIt”, text)
print(search)
Above outputs the list [‘It’]
- \b: It returns the string searched for if it appears at the beginning or at the end of the string the regex is applied on. Note the regex if prefixed with an r, it ensures that the string is being treated as a raw string. E.g.
import re
text = “Cost of living is high in Nigeria”
search1 = re.findall(r“\bCost”, text)
search2 = re.findall(r“Nigeria\b”, text)
print(search1)
print(search2)
The first print statement outputs [‘Cost’] and the second outputs [‘Nigeria’].
- \B: It is the opposite of \b. It returns a list of the matched sequence if the sequence is not at the beginning or end of the string the regex is applied on. e.g.
import re
text = “It is cold outside”
search1 = re.findall(r”outside\B”, text)
search2 = re.findall(r”side\B”, text)
print(search1)
print(search2)
The first print statement outputs an empty list [] because the string has outside at the end, and the second print statement outputs [‘side’] because the string has outside at the end and not side.
- \d: It returns a list of numbers found in the text the regex is applied on. e.g.
import re
text = “I am 32 years old”
search = re.findall(“\d”, text)
print(search)
Above outputs [‘3’, ‘2’].
- \D: It returns a list of characters (including white spaces) that are not numbers in the text the regex is applied on. E.g.
import re
search = re.findall(“\D”, “I am 32 years”)
print(search)
Above outputs [‘I’, ‘ ‘, ‘a’, ‘m’, ‘ ‘, ‘ ‘, ‘y’, ‘e’, ‘a’, ‘r’, ‘s’]
- \s: It returns all the white space characters contained in the string the regex is applied on. e.g.
import re
search = re.findall(“\s”, “I am 32”)
print(search)
Above outputs [‘ ‘, ‘ ‘]
- \S: It returns all the non-white space characters contained in the string the regex is applied on. e.g.
import re
search = re.findall(“\S”, “I am 32”)
print(search)
Above outputs [‘I’, ‘a’, ‘m’, ‘3’, ‘2’]
- \w: It returns all the word characters (i.e. letter a-z A-Z, digits 0–9 and the underscore) contained in the string the regex is applied on. e.g.
import re
search = re.findall(“\w”, “I am 3_2”)
print(search)
This outputs [‘I’, ‘a’, ‘m’, ‘3’, ‘_’, ‘2’]
- \W: It returns all the components of the string that are not word characters (a-z A-Z, 0–9 and _) including whitespaces. e.g.
import re
search = re.findall(“\W”, “I am 3_2!!”)
print(search)
Above outputs [‘ ‘, ‘ ‘, ‘!’, ‘!’]
- \Z: It returns the search string if it is at the end of the string the regex is applied on. e.g.
import re
search = re.findall(“Febe\Z”, “I am Febe”)
print(search)
The output above is [“Febe”]
3. . : Used to denote the number of characters to match in between two or more characters, at the beginning of a character or at the end of a character. E.g.
import re
text = “Cost of living is high in Nigeria”
search1 = re.findall(“…t”, text)
search2 = re.findall(“C…”, text)
Get Opara Febechukwu C.’s stories in your inbox
Join Medium for free to get updates from this writer.
search3 = re.findall(“C..t”, text)
print(search1)
print(search2)
print(search3)
Above three print statements print [‘Cost’]
4. ^: It returns the pattern if the string starts with the search sequence (pattern). E.g.
import re
search = re.findall(“^Fr”, “Freedom is on Friday”)
print(search)
This outputs [‘Fr’] because our string starts with Freedom that bears ‘Fr’.
5. $: It returns the pattern if the string ends the search sequence. E.g.
import re
search = re.findall(“day$”, “It is Friday”)
print(search)
This outputs [‘day’] because our string ends with Friday that has ‘day’ at the end.
6. *: It is used to match a sequence of zero or more occurrence of characters. E.g.
import re
search1 = re.findall(“Febe*”, “I am Febe”)
search2 = re.findall(“Febe..*”, “I am Febechukwu”)
print(search1)
print(search2)
The first print statement outputs ‘Febe’ and the second print statement outputs ‘Febechukwu’.
7. +: It used to match one or more occurrence of characters, e.g.
import re
search = re.findall(“Febe+”, “I am Febec”)
print(search)
Above outputs ‘Febec’ because + means 1 or more and we have atleast 1 character after ‘Febe’
8. ?: It is used for matching zero or one occurrences of a character. e.g.
import re
search = re.findall(“Feb.?”, “I am Febe”)
print(search)
This outputs [‘Febe’]
9. {}: It is used to match exactly the number of specified characters at a minimum value and a maximum value. E.g.
import re
search = re.findall(“Febe.{6}”, “He scolded Febechukwu for not cleaning”)
print(search)
This outputs [‘Febechukwu’]
10.|: Used to validate if a string contains either one word or the other. E.g.
import re
search = re.findall(“Opara|Febechukwu”, “She is Opara Febechukwu”)
print(search)
This outputs [‘Opara’, ‘Febechukwu’]
11. () : Used to group sub-patterns. E.g. (a|b|c)xz match any string that has a or b or c followed by xz. It returns the matched option in the parenthesis. E.g.
import re
search = re.findall(“(Opa|pa|a)ra”, “Opara Febechukwu”)
print(search)
Above outputs [‘Opa’] because we have Opara as the part of the string that starts with Opa and ends with ra, so it returns the matched string which Opa.
Python Regex Functions
The set of python functions used for regular expression matching include:
1. findall 2. search 3. split 4. sub
findall(): It returns a list of all matches e.g.
import re
text = “The weather is hot, just as the tea is hot”
search = re.findall(“ot”, text)
print(search)
It output [“ot”, “ot”] because we have hot twice that contains the search pattern “ot”. If no match is found an empty list is returned.
search(): It searches the string for a match and returns a match object if a match is found or none if no match is found.
A match object is an object bearing information about the search and the result. Some of the methods and properties of a match object include:
1. span(): It returns a tuple containing the start and end positions of the match e.g.
import re
search = re.search(r”\bY\w+”, “The bag is Yellow in colour”)
print(search.span())
The above code outputs the tuple (11,17) because the match was found at the start index of 11 and ends at index 17.
2. string: It is an attribute that returns the string the regex is applied on, e.g.
import re
search = re.search(r”\bY\w+”, “The bag is Yellow in colour”)
print(search.string)
Above outputs “The bag is Yellow in colour”
3. group(): It returns the part of the string where there was a match, e.g.
import re
search = re.search(r”\bY\w+”, “The bag is Yellow in colour”)
print(search.group())
The above code outputs ‘Yellow’.
4. split(): It returns a list that bears entries where the string has been split at each match, e.g.
import re
search = re.split(r“ain\b”, “The rain in Spain”)
print(search)
The above code outputs [‘The r’, ‘ in Sp’, ‘ ‘]
5. sub(): The function replaces all occurrence with a specified string, e.g.
import re
search = re.sub(‘\s’, “-“, “bio data”)
print(search)
The above code matches every white space and replaces it with a hyphen, so it will out “bio-data”.
The number of replacements can be specified using the count parameter e.g.
import re
search = re.sub(‘\s’, “-“, “Bio data of Febechukwu”, 1)
print(search)
The above outputs the string “Bio-data of Febechukwu” if we hadn’t specified the number of match to perform and replace as 1, every white space would have been replaced with hyphen resulting to “Bio-data-of-Febechukwu”.
Application of Knowledge Gained
Now we would try to write the regex to match a password with these specifications:
1. Starts with a capital
2. Contains other alphabets which must be a lowercase and then we can have a mixture of uppercase letters followed by numbers
3. Ends with special symbols
4. Must be of length 8 minimum and length 20 maximum.
Condition 1: ^[A-Z]+
Above we used the set metacharacter and the + metacharacter to specify that our string will start with an uppercase letter ranging from A-Z and there will be one or more of such characters.
Condition 2: ^[A-Z]+[a-z]+[a-zA-Z]+\d+
Here we are specifying that the one or more uppercase letters at the beginning of the password can be followed by one or more lower case letters and a mixture of either uppercase letters or lowercase letters, followed by one or more digits (we are using a special sequence here).
Condition 3: ^[A-Z]+[a-z]+[a-zA-Z]+\d+[%$#!*&|/\^+~@?><]+$
The regex is using set of special characters and is using a metacharacter to specify that it can be one or more.
Condition 4: We can use an if statement to validate further by checking the length of the output, so our code will be something like below:
import re
search = re.findall(‘^([A-Z]+[a-z]+[a-zA-Z]+\d+[%$#!*&|/\^+~@?><]+)$’, “ZebraCrossing2021*#$”)
if len(search) > 0:
search_string = search[0]
else:
search_string = ‘’
length = len(search_string)
if length >=8 and length <=20:
print(‘Password ok!’)
else:
print(‘Password not ok!’)





