0

This is a line of regex from a Python thing I'm writing:

m = re.match(r"{(.+)}", self.label)

As far as i can tell, it's working fine.

Anyways, my question is about the r character before the first double quote. I've never really questioned it. But why is it there? What is its purpose?

michen00
  • 764
  • 1
  • 8
  • 32
  • 1
    Since this is closed (dubious; the supposed answer is not an answer to this question), I'll post my answer as a comment. The `r` before the opening quote means what follows is a raw string literal. In those raw string literals, a backslash mean just that, a backslash. Many regular expressions are loaded with backslashes (yours isn't). Having to double up those backslashes is a royal pain, hence the raw string literals. It's common practice to always use `r""` with regular expressions, even if it doesn't do anything (as in your example). – David Hammen Aug 11 '15 at 22:59

2 Answers2

5

The r before a string literal tells Python not to do any \ escaping on the string. For instance:

>>> print('a\nb')
a
b
>>> print(r'a\nb')
a\nb
>>> 

The reason r-prefixed strings are often used with regular expressions is because regular expressions often use a lot of \'s. For instance, to use a simple example, compare the regular expression '\\d+' versus r'\d+'. They're actually the same string, just represented in different ways. With the r syntax, you don't have to escape the \'s that are used in the regular expression syntax. Now imagine having a lot of \'s in your regular expression; it's much cleaner to use the r syntax.

Cyphase
  • 11,502
  • 2
  • 31
  • 32
2

"String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences."

https://docs.python.org/2/reference/lexical_analysis.html#string-literals

Alex Jadczak
  • 550
  • 3
  • 11