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:)

2 Likes

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

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.

From https://www.regular-expressions.info/charclass.html#:~:text=In%20most%20regex%20flavors%2C%20the,be%20escaped%20by%20a%20backslash.
The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash.

2 Likes