When you use .
and -
in character classes, they both behave differently. In character classes, .
has no special meaning and will be treated as dot only, where as -
can be used to specify ranges like a-zA-Z0-9
.
Since we don't use -
to represent a range in this case, both .
and -
will be matching themselves only. They will not have special meanings.
Also note that, \w
can be defined as [a-zA-Z0-9_]
. It matches only underscore character (_
) not the dash (-
).
Quoting from the Python RegEx documentation,
\w
When the
LOCALE
andUNICODE
flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set[a-zA-Z0-9_]
. WithLOCALE
, it will match the set[0-9_]
plus whatever characters are defined as alphanumeric for the current locale. IfUNICODE
is set, this will match the characters[0-9_]
plus whatever is classified as alphanumeric in the Unicode character properties database.