#Python RegEx
A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.
#RegEx Module
Python has a built-in package called re, which can be used to work with Regular Expressions.
Import the re module:
pythonimport re
#RegEx in Python
When you have imported the re module, you can start using regular expressions:
Example Search the string to see if it starts with "The" and ends with "Spain":
pythonimport re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
#RegEx Functions
The re module offers a set of functions that allows us to search a string for a match:
| Function | Description |
|---|---|
findall | Returns a list containing all matches |
search | Returns a Match object if there is a match anywhere in the string |
split | Returns a list where the string has been split at each match |
sub | Replaces one or many matches with a string |
#Metacharacters
Metacharacters are characters with a special meaning:
| Character | Description | Example | ||
|---|---|---|---|---|
[] | A set of characters | "[a-m]" | ||
\ | Signals a special sequence (can also be used to escape special characters) | "\d" | ||
. | Any character (except newline character) | "he..o" | ||
^ | Starts with | "^hello" | ||
$ | Ends with | "planet$" | ||
* | Zero or more occurrences | "he.*o" | ||
+ | One or more occurrences | "he.+o" | ||
? | Zero or one occurrences | "he.?o" | ||
{} | Exactly the specified number of occurrences | "he.{2}o" | ||
| `\ | ` | Either or | `"falls\ | stays"` |
() | Capture and group |
#Flags
You can add flags to the pattern when using regular expressions.
| Flag | Shorthand | Description |
|---|---|---|
re.ASCII | re.A | Returns only ASCII matches |
re.DEBUG | Returns debug information | |
re.DOTALL | re.S | Makes the . character match all characters (including newline) |
re.IGNORECASE | re.I | Case-insensitive matching |
re.MULTILINE | re.M | Returns matches at the start/end of each line |
re.NOFLAG | Specifies that no flag is set for this pattern | |
re.UNICODE | re.U | Returns Unicode matches (Default in Python 3) |
re.VERBOSE | re.X | Allows whitespaces and comments inside patterns |
#Special Sequences
A special sequence is a \ followed by one of the characters in the list below:
| Character | Description | Example |
|---|---|---|
\A | Returns a match if characters are at the beginning of the string | "\AThe" |
\b | Returns a match at the beginning or end of a word | r"\bain" |
\B | Returns a match where characters are present, but NOT at the start/end | r"\Bain" |
\d | Returns a match where the string contains digits (0-9) | "\d" |
\D | Returns a match where the string DOES NOT contain digits | "\D" |
\s | Returns a match where the string contains a white space character | "\s" |
\S | Returns a match where the string DOES NOT contain a white space | "\S" |
\w | Returns a match containing word characters (a-Z, 0-9, and \_) | "\w" |
\W | Returns a match where the string DOES NOT contain word characters | "\W" |
\Z | Returns a match if the characters are at the end of the string | "Spain\Z" |
#Sets
A set is a set of characters inside a pair of square brackets [] with a special meaning:
| Set | Description |
|---|---|
[arn] | Returns a match where one of the specified characters (a, r, or n) is present |
[a-n] | Returns a match for any lower case character, alphabetically between a and n |
[^arn] | Returns a match for any character EXCEPT a, r, and n |
[0123] | Returns a match where any of the specified digits (0, 1, 2, or 3) are present |
[0-9] | Returns a match for any digit between 0 and 9 |
[0-5][0-9] | Returns a match for any two-digit numbers from 00 and 59 |
[a-zA-Z] | Returns a match for any character alphabetically between a and z |
[+] | In sets, special characters lose meaning; [+] matches a literal + |
#The findall() Function
The findall() function returns a list containing all matches.
Example: Print a list of all matches
pythonimport re
txt = "The rain in Spain"
x = re.findall("ai", txt)
print(x)
If no matches are found, an empty list is returned.
#The search() Function
The search() function searches the string for a match and returns a Match object. If there is more than one match, only the first occurrence is returned.
Example: Search for the first white-space character
pythonimport re
txt = "The rain in Spain"
x = re.search("\s", txt)
print("The first white-space character is located in position:", x.start())
#The split() Function
The split() function returns a list where the string has been split at each match.
Example: Split at each white-space character
pythonimport re
txt = "The rain in Spain"
x = re.split("\s", txt)
print(x)
You can control the number of occurrences by specifying the maxsplit parameter.
#The sub() Function
The sub() function replaces the matches with the text of your choice.
Example: Replace every white-space character with the number 9
pythonimport re
txt = "The rain in Spain"
x = re.sub("\s", "9", txt)
print(x)
#Match Object
A Match Object is an object containing information about the search and the result. If there is no match, the value None will be returned.
#Methods and Properties:
.span()returns a tuple containing the start and end positions of the match..stringreturns the string passed into the function..group()returns the part of the string where there was a match.
Example: Using Match object methods
pythonimport re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span()) # Returns (12, 17)
print(x.string) # Returns "The rain in Spain"
print(x.group()) # Returns "Spain"