What's the difference between an r string (r'foobar'
) and a normal string ('foobar'
) in Python? Is r'string'
a regex string?
I've tried the following and there aren't any effects on my regex matches:
>>> import re
>>> n = 3
>>> rgx = '(?=('+'\S'*n+'))'
>>> x = 'foobar'
>>> re.findall(rgx,x)
['foo', 'oob', 'oba', 'bar']
>>>
>>> rgx2 = r'(?=('+'\S'*n+'))'
>>> re.findall(rgx2,x)
['foo', 'oob', 'oba', 'bar']
>>>
>>> rgx3 = r'(?=(\S\S\S))'
>>> re.findall(rgx3,x)
['foo', 'oob', 'oba', 'bar']
r
doesn't signify a "regex string"; it means "raw string". As per the docs:
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.
They are commonly used (and recommended) for regular expressions because regex and non-raw strings both use backslashes as an escape character. For example, to match a literal backslash with a regex in a normal string would be '\\\\'
; using a raw string, it's just '\\'
.