python-regex.md

#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:

FunctionDescription
findallReturns a list containing all matches
searchReturns a Match object if there is a match anywhere in the string
splitReturns a list where the string has been split at each match
subReplaces one or many matches with a string

#Metacharacters

Metacharacters are characters with a special meaning:

CharacterDescriptionExample
[]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.

FlagShorthandDescription
re.ASCIIre.AReturns only ASCII matches
re.DEBUGReturns debug information
re.DOTALLre.SMakes the . character match all characters (including newline)
re.IGNORECASEre.ICase-insensitive matching
re.MULTILINEre.MReturns matches at the start/end of each line
re.NOFLAGSpecifies that no flag is set for this pattern
re.UNICODEre.UReturns Unicode matches (Default in Python 3)
re.VERBOSEre.XAllows whitespaces and comments inside patterns

#Special Sequences

A special sequence is a \ followed by one of the characters in the list below:

CharacterDescriptionExample
\AReturns a match if characters are at the beginning of the string"\AThe"
\bReturns a match at the beginning or end of a wordr"\bain"
\BReturns a match where characters are present, but NOT at the start/endr"\Bain"
\dReturns a match where the string contains digits (0-9)"\d"
\DReturns a match where the string DOES NOT contain digits"\D"
\sReturns a match where the string contains a white space character"\s"
\SReturns a match where the string DOES NOT contain a white space"\S"
\wReturns a match containing word characters (a-Z, 0-9, and \_)"\w"
\WReturns a match where the string DOES NOT contain word characters"\W"
\ZReturns 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:

SetDescription
[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:

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"