Python RegEx
Python RegEx
In this tutorial, you will learn about regular expressions (RegEx), and use Python's re module to
work with RegEx (with the help of examples).
A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. For
example,
^a...s$
The above code defines a RegEx pattern. The pattern is: any five letter string starting with a
and ending with s.
import re
pattern = '^a...s$'
test_string = 'abyss'
result = re.match(pattern, test_string)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
Run Code
Here, we used re.match() function to search pattern within the test_string. The method returns
a match object if the search is successful. If not, it returns None.
Specify Pattern Using RegEx
To specify regular expressions, metacharacters are used. In the above example, ^ and $ are
metacharacters.
MetaCharacters
Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's a
list of metacharacters:
[] . ^ $ * + ? {} () \ |
[] - Square brackets
Here, [abc] will match if the string you are trying to match contains any of the a, b or c.
You can also specify a range of characters using - inside square brackets.
You can complement (invert) the character set by using caret ^ symbol at the start of a square-
bracket.
^ - Caret
The caret symbol ^ is used to check if a string starts with a certain character.
$ - Dollar
The dollar symbol $ is used to check if a string ends with a certain character.
* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.
The plus symbol + matches one or more occurrences of the pattern left to it.
? - Question Mark
The question mark symbol ? matches zero or one occurrence of the pattern left to it.
{} - Braces
Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to
it.
Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but not more
than 4 digits
() - Group
Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that
matches either a or b or c followed by xz
\ - Backslash
Backlash \ is used to escape various characters including all metacharacters. For example,
\$a match if a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in a
special way.
If you are unsure if a character has special meaning or not, you can put \ in front of it. This
makes sure the character is not treated in a special way.
Special Sequences
Special sequences make commonly used patterns easier to write. Here's a list of special
sequences:
\B- Opposite of \b. Matches if the specified characters are not at the beginning or end of a
word.
\w- Matches any alphanumeric character (digits and alphabets). Equivalent to [a-zA-Z0-9_].
By the way, underscore _ is also considered an alphanumeric character.
Tip: To build and test regular expressions, you can use RegEx tester tools such as regex101.
This tool not only helps you in creating regular expressions, but it also helps you learn it.
Now you understand the basics of RegEx, let's discuss how to use RegEx in your Python code.