Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

Is the backslash in front of "." optional ? regex

When I read about character classes in the previous mission I see that the “.” class has no backslash in front of it. However, in the solution to this problem, DQ uses a backslash r"[Pp]ython ([\d\.]+)"

Is the backslash optional or is it a typo in the previous mission ?

Hello @nicolas_mtl! I’m not sure which mission you’re talking about. I cannot see your code in any of the solutions.

However, to answer your question: the special symbol . (dot) matches any character (except new line) but if you want to match only the dot (as literal character) you have to escape it with a backslash. It’s also valid for other special symbols like $, ^ ecc. You can read more about it here.

Happy coding:)


You can remove the backslash in \. to . and r"[Pp]ython ([\d.]+)" should still work the same.
You can do a quick test on

Mouseover your regex and it should say “x matches the character x literally” vs a wildcard outside character class [] which says “x matches any character (except for line terminators)”. You can further confirm this by inserting random characters after python (eg. python 3.a.3) and see that the . inside [] does not match extra a and is indeed not a wildcard.

The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash.