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

Function of r" "

screen link: https://app.dataquest.io/m/354/regular-expression-basics/7/accessing-the-matching-text-with-capture-groups

Could somebody explain what r" " actually does ?
Thank you very much beforehand!

r denotes raw string. They treat the string as it is, that is, they do not process escape characters.

DataQuest also gave a brief explanation in that page you posted…

This can make regular expressions even more difficult to read and interpret, so instead we use raw strings, which we denote by prefixing our string with the r character. Let’s take a look at the code from above with a raw string:

 print(r'hello\b world')
 hello\b world

We strongly recommend using raw strings for every regex you write, rather than remember which sequences are escape sequences and using raw strings selectively. That way, you’ll never encounter a situation where you forget or overlook something which causes your regex to break.

More

1 Like

What does parentheses do here: pattern = r"([\w+])"
does parentheses prevent r" " from treating the pattern as raw string
that is why there \ is working. but here : pattern = r"[(\w+)]" —> \ is not working because r" " accepting the string as just raw one.
Am I right?

1 Like