first you have to know the character class
\d = Any numeric digit from 0 to 9.
\D = Any character that is not a numeric digit from 0 to 9.
\w = Any letter, numeric digit, or the underscore character.(Think of this as matching “word” characters.)
\W = Any character that is not a letter, numeric digit, or the underscore character.
\s = Any space, tab, or newline character. (Think of this as matching “space” characters.)
\S = Any character that is not a space, tab, or newline.
an example of date validation:
import re
regex = re.compile(r"\d\d/\d\d/\d\d\d\d")
match = regex.search("Mary's date of birth: 10/04/2001")
print(match.group())
First we have to import the library is, a library comes by default in python, I used re.compile and as a parameter I passed a String with the pattern we want to look for, the first \d\d represent the two digits of the month, then I typed a /, then \d\d for the day, then a /, then \d\d\d\d for the year, this function returns a Regex object.
On the other line I use regex.search and pass the String where I want to search for the pattern, regex in this case is the name of the variable where I stored the pattern. This will return a Match object.
On the other line I used print(match.group()). The Match object's group method will only return the first match of the pattern in the String. The output of this code:
10/04/2001
But, if there is more than one date in the String, it will only return the first, to solve this we use the findall() method, this method returns a list with all matches of the pattern found:
import re
regex = re.compile(r"\d\d/\d\d/\d\d\d\d")
result = regex.findall("Mary's birth date: 10/04/2001, John's birth date: 04/21/1989")
print(result)
the output of this code:
['10/04/2001', '04/21/1989']
you separate your pattern into different groups using parentheses (), as in the example:
import re
regex = re.compile(r"(\d\d)/(\d\d)/(\d\d\d\d)")
match = regex.search("Mary's birth date: 10/04/2001")
print(match.group())
the output of this code is 10/04/2001, but if we pass 1 as a parameter of match.group() the output will be 10 because it is the first match of the first group of the String, if we pass 2 as a parameter the output will be 04 and if you pass 3 as a parameter the output will be 2001.
If you use findall() on patterns that have groups, the output will be a list and in that list will have a tuple with each group's match:
import re
regex = re.compile(r"(\d\d)/(\d\d)/(\d\d\d\d)")
result = regex.findall("Mary's birth date: 10/04/2001")
print(result)
the output of this code:
[('10', '04', '2001')]
all of this below works with groups too:
you can make patterns with a certain amount of characters using braces {}, for example, we want to do a validation of a number that has 10 digits, instead of typing \d 10 times, we can do that using \d{10}:
import re
regex = re.compile(r"\d{10}")
result = regex.findall("a random book has 6295618392 letters")
print(result)
the output of this code:
['6295618392']
You can set a minimum and a maximum of characters too, for example if you want a number of 5 to 10 digits is just typing \d{5,10}, you can also not set a minimum and not set a maximum, number with 10 digits or less \d{,10}, number with 10 digits or more \d{10,}.
Use ? to make it optional, for example:
import re
regex = re.compile(r"books?")
result = regex.findall("Mary has only one book")
print(result)
the output of this code:
['book']
Or:
import re
regex = re.compile(r"books?")
result = regex.findall("Mary has many books")
print(result)
the output of this code:
['books']
Since ? is after s, s is optional, making the String "book" or the String "books" valid.
Using | you can search for either a String or another String, for example, if you type Mary|John it will validate the String Mary or the String John, whichever comes first.
import re
regex = re.compile(r"Mary|John")
result = regex.findall("Mary has many books")
print(result)
result = regex.findall("John has many books")
print(result)
the output of this code:
['Mary']
['John']
Using + means it has to have 1 or more characters or groups if you are using it in groups, in this example I first use the String "ILovePython", after I use the String "I Love Python":
import re
regex = re.compile(r"\s+Python")
result = regex.findall("ILovePython")
print(result)
result = regex.findall("I Love Python")
print(result)
the output of this code:
[]
[' Python']
in the first print, result is equal to [] because a space character was not found before the String Python, in the second print, as a space character was found before the String Python, result is equal to [' Python'].
* is similar to +, the only difference is that using * can be optional, that is, 0 or more characters, whereas with + it must have at least 1 character:
import re
regex = re.compile(r"\s*Python")
result = regex.findall("ILovePython")
print(result)
result = regex.findall("I Love Python")
print(result)
the output of this code:
['Python']
[' Python']
On the first print it returned ['Python'], though not without a space before the word.
You can create your own character classes using []:
import re
regex = re.compile(r"[aeiouAEIOU]")
result = regex.findall("Hello World")
print(result)
the output of this code:
['e', 'o', 'o']
[aeiouAEIOU] represents 1 character, the character can be one of those inside the [], that is, all the vowels, that's why the output shows all the vowels of the string "Hello World"
using ^ before, the reverse is valid:
import re
regex = re.compile(r"[^aeiouAEIOU]")
result = regex.findall("Hello World")
print(result)
the output of this code:
['H', 'l', 'l', ' ', 'W', 'r', 'l', 'd']
that is, characters that are not the ones inside the [] which in the example is aeiouAEIOU are the ones that will be valid.
can be used [a-zA-Z] to represent all characters of the alphabet.
can be used [0-9] to represent digits from 0 to 9, but using that or \d is the same thing, can be used to represent smaller intervals like [0-5].
you can also use ^ at the beginning of your patterns, this will indicate that the String has to contain the pattern at its beginning:
import re
regex = re.compile(r"^Hello")
result = regex.findall("Hello, World")
print(result)
result = regex.findall("World, Hello")
print(result)
the output of this code:
['Hello']
[]
in the first print ['Hello'] is returned because "Hello" was found at the beginning of the search string.
you can use $ at the end of the pattern, this indicates that the String has to contain the pattern at its end:
import re
regex = re.compile(r"World$")
result = regex.findall("Hello, World")
print(result)
result = regex.findall("World, Hello")
print(result)
the output of this code:
['World']
[]
in the first print ['World'] is returned because "World" was found at the end of the search string.
The . is called a wildcard character, that is, it is equivalent to any single character:
import re
regex = re.compile(r".at")
result = regex.findall("the cat use the hat")
print(result)
the output of this code:
['cat', 'hat']
Using re.VERBOSE as a second parameter of the re.compile() method, you can add line breaks, spaces and comments to your pattern, using re.I or re.IGNORECASE you validate both uppercase and lowercase letters:
import re
regex = re.compile(r"""
^=SUM\(
([A-X][A-F][A-D]|[A-Z]{1,2})
(10[0-4][0-8][0-5][0-7][0-6]|[0-9]{2,6}|[1-9])?
(:([A-X][A-F][A-D]|[A-Z]{1,2})
(10[0-4][0-8][0-5][0-7][0-6]|[0-9]{2,6}|[1-9])?)?
(
,([A-X][A-F][A-D]|[A-Z]{1,2})
(10[0-4][0-8][0-5][0-7][0-6]|[0-9]{2,6}|[1-9])?
(:([A-X][A-F][A-D]|[A-Z]{1,2})
(10[0-4][0-8][0-5][0-7][0-6]|[0-9]{2,6}|[1-9])?)?
)*
\)$
""",
re.I | re.VERBOSE
)
This validates Excel's SUM formula, validates =sum(a1:b5) and also =SUM(A1:B5), that's because I used re.I, I used re.VERBOSE so I can add line breaks, I can use both at the same time using | to separate re.I from re.VERBOSE.
Using re.DOTALL as the second parameter of re.compile(), the . becomes equivalent to line breaks as well:
import re
regex = re.compile(r".new_line", re.DOTALL)
result = regex.findall("""first_line
new_line
""")
print(result)
the output of this code:
[' new_line]
There was a line break after first_line, the . corresponded to this line break, then it was returned line break with the String new_line.