Question
Recap the following regular expression concepts (more details in the article below):
- The dot character
.
in regular expressions matches any character excluding the newline character. For example, the pattern'c.t'
will match the strings'cat'
,'cut'
, or'czt'
. - The character class
[ ]
is a set of characters: if you use it in a regex pattern, the engine will match exactly one character from the set. For example, the pattern'c[auz]t'
will match the strings'cat'
,'cut'
, or'czt'
. - Special characters such as the dot character often need to be escaped in a regex pattern if you want to match them. For example, to match the actual dot
'.'
character, you need to design a pattern with escaped dot'\.'
. In other words, the pattern'hello\.'
would match the string'hello.'
but not the string'hello!'
.
Question:
Do you need to escape the dot character in a Python regex character class?
Answer
No, you don’t need to escape the dot character in a character class. This holds for the Python re
and the newer Python regex
modules.
The reason is that in a character class, any character except ^
, -
, ]
or \
are literals, i.e., they’ve lost their special meaning if they had any.
The Minus Character
For example, the minus '-'
character has a special meaning within the character class, it’s the range character in the pattern '[a-z]'
.
However, the minus is also seen as a normal literal character if it’s the first or last value in a character class. Python knows that the minus as the first or last character cannot signal a range because the range wouldn’t be opened or closed (e.g., patterns [-z]
or [a-]
).
The Hat Character
The hat special character '^'
means start-of-the-line regex. It has another special meaning when used as the first character of the character class (=negative character class).
However, it loses its special meaning when it’s not the first character. So if you want to match the '^'
symbol, you can use it as the non-first character in a character class (e.g., pattern [ab^c]
).
💡 Note: It doesn’t harm to escape the dot regex or any other special symbol within the character class—Python will simply ignore it!
Let’s recap some of the basic concepts in more detail next!
Understanding the Dot Regex
The dot regex .
matches all characters except the newline character.
For example, the regular expression '...'
matches strings 'hey'
and 'tom'
. But it does not match the string 'yo\nto'
which contains the newline character '\n'
. Combined with the asterisk quantifier in the pattern '.*'
, the dot regex matches an arbitrary number of symbols except newline characters.
Learn more in our detailed blog tutorial guide:
Understanding Character Classes
The character set (or character class) is, surprise, a set of characters: if you use a character set in a regular expression pattern, you tell the regex engine to choose one arbitrary character from the set. As you may know, a set is an unordered collection of unique elements. So each character in a character set is unique and the order doesn’t really matter (with a few minor exceptions).
Here’s an example of a character set as used in a regular expression:
>>> import re >>> re.findall('[abcde]', 'hello world!') ['e', 'd']
Learn more in our detailed blog tutorial guide:
Understanding Special Characters
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.