Why do Python regex strings sometimes work without using raw strings?

Python recommends using raw strings when defining regular expressions in a module re. From the Python documentation :

Regular expressions use the backslash character ('\') to indicate special forms or to allow the use of special characters without using their special meaning. This interferes with using Pythons of the same character for the same purpose in string literals; for example, to match a literal backslash, you need to write "\\" as the pattern string, because the regular expression must be \, and each backslash must be expressed as \ inside the regular literal of a Python string.

However, in many cases this is not necessary, and you get the same result whether you use the raw string or not:

$ ipython

In [1]: import re

In [2]: m = re.search("\s(\d)\s", "a 3 c")

In [3]: m.groups()
Out[3]: ('3',)

In [4]: m = re.search(r"\s(\d)\s", "a 3 c")

In [5]: m.groups()
Out[5]: ('3',)

However, in some cases this is not the case:

In [6]: m = re.search("\s(.)\1\s", "a 33 c")

In [7]: m.groups()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-84a8d9c174e2> in <module>()
----> 1 m.groups()

AttributeError: 'NoneType' object has no attribute 'groups'

In [8]: m = re.search(r"\s(.)\1\s", "a 33 c")

In [9]: m.groups()
Out[9]: ('3',)

And you should avoid special characters if you are not using a raw string:

In [10]: m = re.search("\\s(.)\\1\\s", "a 33 c")

In [11]: m.groups()
Out[11]: ('3',)

, , ( [2])?

+4
2

, \s \d escape- python. :

C escape- , .

, , , .

+7

, \s \d escape-:

>>> print('\s')
\s
>>> print('\d')
\d
>>>

, \s \d. \1 escape-:

>>> print('\1')
>>>

, \1.

Python . String Bytes .

+6

All Articles