Given is the following regular expression:
regex = '[a-z.]+'
But what does it match if you place the dot character inside a regex character set?
The answer is that the dot inside the character set matches the dot symbol—and not an arbitrary character. The reason is that the character set removes the special meaning of the dot symbol.
Here’s a simple example:
import re regex = '[a-z.]+' text_1 = 'hello.world' text_2 = 'HELLO.WORLD' print(re.match(regex, text_1)) # <re.Match object; span=(0, 11), match='hello.world'> print(re.match(regex, text_2)) # None
The first text will be matched in both cases (the dot character matches an arbitrary character or the dot symbol).
But the second text will only match if the dot has the meaning: “match an arbitrary character”. Otherwise, the character set cannot match the text.
As the result is None, the text could not have been matched. This proves that the dot metacharacter loses its special meaning inside a character set.
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.