Regular expressions are a strange animal. Many students find them difficult to understand – do you?
I realized that a major reason for this is simply that they don’t understand the special regex characters. To put it differently: understand the special characters and everything else in the regex space will come much easier to you.
Related article: Python Regex Superpower – The Ultimate Guide
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
Let’s start with the absolute first thing you need to know with regular expressions: a regular expression (short: regex) searches for a given pattern in a given string.
What’s a pattern? In its most basic form, a pattern can be a literal character. So the literal characters
'c' are all valid regex patterns.
For example, you can search for the regex pattern
'a' in the string
'hello world' but it won’t find a match. You can also search for the pattern
'a' in the string
'hello woman' and there is a match: the second last character in the string.
Based on the simple insight that a literal character is a valid regex pattern, you’ll find that a combination of literal characters is also a valid regex pattern. For example, the regex pattern
'an' matches the last two characters in the string
However, the power of regular expressions come from their abstraction capability. Instead of writing the character set
[abcdefghijklmnopqrstuvwxyz], you’d write
[a-z] or even
\w. The latter is a special regex character—and pros know them by heart. In fact, regex experts seldomly match literal characters. In most cases, they use more advanced constructs or special characters for various reasons such as brevity, expressiveness, or generality.
So what are the special characters you can use in your regex patterns?
Let’s have a look at the following table that contains all special characters in Python’s
re package for regular expression processing.
|The newline symbol is not a special symbol particular to regex only, it’s actually one of the most widely-used, standard characters. However, you’ll see the newline character so often that I just couldn’t write this list without including it. For example, the regex |
|The tabular character is, like the newline character, not a “regex-specific” symbol. It just encodes the tabular space |
|The whitespace character is, in contrast to the newline character, a special symbol of the regex libraries. You’ll find it in many other programming languages, too. The problem is that you often don’t know which type of whitespace is used: tabular characters, simple whitespaces, or even newlines. The whitespace character |
|The whitespace-negation character matches everything that does not match |
|The word character regex simplifies text processing significantly. It represents the class of all characters used in typical words (|
|The word-character-negation. It matches any character that is not a word character.|
|The word boundary is also a special symbol used in many regex tools. You can use it to match, as the name suggests, the boundary between the a word character (|
|The digit character matches all numeric symbols between 0 and 9. You can use it to match integers with an arbitrary number of digits: the regex |
|Matches any non-digit character. This is the inverse of |
But these are not all characters you can use in a regular expression.
There are also meta characters for the regex engine that allow you to do much more powerful stuff.
A good example is the asterisk operator that matches “zero or more” occurrences of the preceding regex. For example, the pattern
.*txt matches an arbitrary number of arbitrary characters followed by the suffix
'txt'. This pattern has two special regex meta characters: the dot
. and the asterisk operator
*. You’ll now learn about those meta characters:
Regex Meta Characters
Feel free to watch the short video about the most important regex meta characters:
Next, you’ll get a quick and dirty overview of the most important regex operations and how to use them in Python.
Here are the most important regex operators:
|The wild-card operator (dot) matches any character in a string except the newline character |
|The zero-or-more asterisk operator matches an arbitrary number of occurrences (including zero occurrences) of the immediately preceding regex. For example, the regex ‘cat*’ matches the strings |
|The zero-or-one operator matches (as the name suggests) either zero or one occurrences of the immediately preceding regex. For example, the regex ‘cat?’ matches both strings |
|The at-least-one operator matches one or more occurrences of the immediately preceding regex. For example, the regex |
|The start-of-string operator matches the beginning of a string. For example, the regex |
|The end-of-string operator matches the end of a string. For example, the regex |
|The OR operator matches either the regex A or the regex B. Note that the intuition is quite different from the standard interpretation of the or operator that can also satisfy both conditions. For example, the regex |
|The AND operator matches first the regex A and second the regex B, in this sequence. We’ve already seen it trivially in the regex |
Note that I gave the above operators some more meaningful names (in bold) so that you can immediately grasp the purpose of each regex. For example, the
‘^’ operator is usually denoted as the ‘caret’ operator. Those names are not descriptive so I came up with more kindergarten-like words such as the “start-of-string” operator.
Let’s dive into some examples!
import re text = ''' Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. ''' print(re.findall('.a!', text)) ''' Finds all occurrences of an arbitrary character that is followed by the character sequence 'a!'. ['Ha!'] ''' print(re.findall('is.*and', text)) ''' Finds all occurrences of the word 'is', followed by an arbitrary number of characters and the word 'and'. ['is settled, and'] ''' print(re.findall('her:?', text)) ''' Finds all occurrences of the word 'her', followed by zero or one occurrences of the colon ':'. ['her:', 'her', 'her'] ''' print(re.findall('her:+', text)) ''' Finds all occurrences of the word 'her', followed by one or more occurrences of the colon ':'. ['her:'] ''' print(re.findall('^Ha.*', text)) ''' Finds all occurrences where the string starts with the character sequence 'Ha', followed by an arbitrary number of characters except for the new-line character. Can you figure out why Python doesn't find any?  ''' print(re.findall('\n$', text)) ''' Finds all occurrences where the new-line character '\n' occurs at the end of the string. ['\n'] ''' print(re.findall('(Life|Death)', text)) ''' Finds all occurrences of either the word 'Life' or the word 'Death'. ['Life', 'Death'] '''
In these examples, you’ve already seen the special symbol
\n which denotes the new-line character in Python (and most other languages). There are many special characters, specifically designed for regular expressions.
Which Special Python Regex Characters Must Be Escaped?
Short answer: Here’s an exhaustive list of all special characters that need to be escaped:
Question: Is there a comprehensive list of which special characters must be escaped in order to remove the special meaning within the regex?
Example: Say you search for those symbols in a given string and you wonder which of them you must escape:
Answer: Differentiate between using the special symbols within or outside a character class.
- Within the character class, you need to escape only the minus symbol replacing
[\-]as this has a special meaning within the character class (the “range” character).
- Outside the character class in a normal regex pattern, you need to escape only the regex chars with special meaning. Here’s an exhaustive list of all special characters that need to be escaped:
import re text = '|^&+-%*/=!>' # WITHIN CHARACTER CLASS --> ESCAPE '-' print(re.findall('[|^&+\-%*/=!>]', text)) # ['|', '^', '&', '+', '-', '%', '*', '/', '=', '!', '>'] # WITHOUT CHARACTER CLASS --> ESCAPE ALL SPECIAL CHARS '.*?+^$|' pattern = '|^&+$-%*/=!>' print(re.findall('\|', text)) print(re.findall('\^', text)) print(re.findall('\$', text)) print(re.findall('\+', text)) print(re.findall('-', text)) print(re.findall('%', text)) print(re.findall('\*', text)) print(re.findall('/', text)) print(re.findall('=', text)) print(re.findall('!', text)) ''' ['|'] ['^'] ['$'] ['+'] ['-'] ['%'] ['*'] ['/'] ['='] ['!'] '''
By escaping the special regex symbols, they lose their special meaning and you can find the symbols in the original text.
Where to Go From Here
You’ve learned all special characters of regular expressions, as well as meta characters. This will give you a strong basis for improving your regex skills.
If you want to accelerate your skills, you need a good foundation. Check out my brand-new Python book “Python One-Liners (Amazon Link)” which boosts your skills from zero to hero—in a single line of Python code!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.