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 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.