Python RegEx
A regular expression, or RegEx for short, is a series of characters that describes a search pattern.
With one, you can test whether a string contains that pattern.
RegEx Module
Regular expressions in Python are handled by re, a built-in package.
Start by importing it:
Example
import re
RegEx in Python
Once re is imported, regular expressions are ready to use:
Example
re module and search a string to see if it starts with "The" and ends with "Spain".
Use re.search() with the pattern "^The.*Spain$".
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
RegEx Functions
To look for matches in a string, the re module gives you these functions:
findall- Gives back a list of every matchsearch- Gives back a Match object when a match exists anywhere in the stringsplit- Cuts the string at every match and gives back a list of the piecessub- Swaps one or many matches for a string of your choice
Metacharacters
A metacharacter is a character that carries a special meaning:
[]- Defines a set of characters\- Starts a special sequence, or escapes a special character.- Matches any character other than a newline^- Matches at the start$- Matches at the end*- Zero or more repeats+- One or more repeats?- Zero or one repeat{}- The exact number of repeats you specify|- One option or the other()- Captures and groups
Flags
Flags added to a pattern change how a regular expression behaves.
re.ASCII(re.A) - Matches ASCII characters onlyre.DEBUG- Shows debug informationre.DOTALL(re.S) - Lets the . character match everything, newlines includedre.IGNORECASE(re.I) - Ignores upper and lower case when matchingre.MULTILINE(re.M) - Matches at the start and end of every linere.NOFLAG- Says that this pattern has no flag setre.UNICODE(re.U) - Gives Unicode matchesre.VERBOSE(re.X) - Permits whitespace and comments inside a pattern
Special Sequences
Put a \ in front of certain characters and you get a special sequence, which has its own special meaning. These are the ones available:
\A- Finds a match only when the given characters open the string\b- Finds a match when the given characters begin or end a word\B- Finds a match when the given characters appear, but NOT at the beginning or end of a word\d- Finds a match for any digit (0-9) in the string\D- Finds a match for anything in the string that is NOT a digit\s- Finds a match for any white space character in the string\S- Finds a match for anything in the string that is NOT white space\w- Finds a match for any word character in the string\W- Finds a match for anything in the string that is NOT a word character\Z- Finds a match only when the given characters close the string
Sets
A set is a group of characters between square brackets [], and it too has a special meaning:
[arn]- Matches one of the listed characters: a, r or n[a-n]- Matches any lowercase letter that falls between a and n in the alphabet[^arn]- Matches any character other than a, r and n[0123]- Matches one of the listed digits: 0, 1, 2 or 3[0-9]- Matches any single digit from 0 through 9[0-5][0-9]- Matches any two-digit number from 00 through 59[a-zA-Z]- Matches any letter from a to z, whether lower case OR upper case[+]- Inside a set, the characters +, *, ., |, (), $ and {} lose their special meaning, so [+] just looks for a literal + in the string
The findall() Function
The findall() function collects all the matches into a list, which keeps them in the order they were found.
When nothing matches, you get an empty list:
Example
re.findall() to find all occurrences of "ai" in "The rain in Spain".
Print the list of matches.
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)
Example
re.findall() to search for "Portugal" in "The rain in Spain".
Print the result (empty list when no match).
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.findall("Portugal", txt)
print(x)
The search() Function
The search() function looks through the string and, when it finds a match, gives you a Match object.
Several matches in the string make no difference, because you only get the first one:
When nothing matches, you get the value None:
Example
re.search() to find the first white-space character in "The rain in Spain".
Print its position using .start().
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.search("\s", txt)
print("The first white-space character is located in position:", x.start())
Example
re.search() to search for "Portugal" in "The rain in Spain".
Print the result (None when no match).
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.search("Portugal", txt)
print(x)
The split() Function
The split() function breaks the string at each match and returns the pieces in a list:
The maxsplit parameter limits how many splits are made:
Example
re.split() to split "The rain in Spain" at each white-space character.
Print the resulting list.
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.split("\s", txt)
print(x)
Example
re.split() to split "The rain in Spain" at the first occurrence only (maxsplit=1).
Print the result.
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.split("\s", txt, 1)
print(x)
The sub() Function
The sub() function puts text of your choosing in place of each match:
The count parameter limits how many replacements are made:
Example
re.sub() to replace every white-space with "9" in "The rain in Spain".
Print the result.
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.sub("\s", "9", txt)
print(x)
Example
re.sub() to replace the first 2 white-space characters with "9" in "The rain in Spain".
Print the result.
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.sub("\s", "9", txt, 2)
print(x)
Match Object
A Match Object holds details about a search and what it found.
Note: When nothing matches, you get the value None and no Match Object at all.
To get at those details, use the object's properties and methods:
.span()gives a tuple holding where the match starts and where it ends.stringgives back the string you passed to the function.group()gives back the matching text
Example
re.search() to search for "ai" in "The rain in Spain".
Print the Match object returned.
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.search("ai", txt)
print(x)
Example
r"\bS\w+" in "The rain in Spain".
Print the span of the match using .span().
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
Example
r"\bS\w+" in "The rain in Spain".
Print the original string using .string.
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
Example
r"\bS\w+" in "The rain in Spain".
Print the matched text using .group().
Output: complete Python program
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())