The Dot Character in a Character Set – What Does It Match?

Rate this post

Given is the following regular expression:

regex = '[a-z.]+'

Note the dot character inside the character set. As you may know, the dot metacharacter matches an arbitrary character if it is used outside a character set.

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 = ''
text_2 = 'HELLO.WORLD'

print(re.match(regex, text_1))
# <re.Match object; span=(0, 11), match=''>

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.