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
- 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
- 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
Do you need to escape the dot character in a Python regex character class?
No, you don’t need to escape the dot character in a character class. This holds for the Python
re and the newer Python
The reason is that in a character class, any character except
\ 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
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
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
💡 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
'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.
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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.