Backslash in raw strings

In the Advanced Regular Expressions course exercise the URLs, I don’t understand why the solution includes backslashes to escape special characters in a raw string. I thought that the whole purpose of using raw strings in python was to avoid dealing with that?

For exemple, in:

pattern = r"(https?)://([\w\.\-]+)/?(.*)"

See how the solution escapes the “.” and the “+” characters? Since it is inside a raw string, won’t the solution code just try to look for a " \ " literal character in the URLs instead of using it as an escape?

This is what I thought would be the correct answer (it works in the code too by the way).

pattern = r"(https?)://([\w.-]+)/?(.*)"

Thanks for lighting up my bubble!

G

3 Likes

I am sure this will clear the concept how it works

Yes this is also correct answer but not because of you used raw string it’s because of they are inside [] brackets.

However , inside square brackets many symbols (like + and . ) lose their special meaning, so there’s no need to escape them. See this thread for more - 369-5 Reg Ex Making my Brain Hurt!

4 Likes