light2yellow light2yellow - 3 months ago 10
Python Question

re.sub() documentation misunderstanding

I've just started learning regular expressions and the documentation for

re.sub()
states:


Changed in version 3.5: Unmatched groups are replaced with an empty
string.

Deprecated since version 3.5, will be removed in version 3.6: Unknown
escapes consist of '\' and ASCII letter now raise a deprecation
warning and will be forbidden in Python 3.6.


Is re.sub() deprecated? What should I use then?

Answer

You misunderstand the documentation. The re.sub() function is not deprecated. The deprecation warning concerns specific syntax.

Earlier in the re.sub() documentation you'll find this:

Unknown escapes such as \& are left alone.

If you used and unknown escape with an ASCII letter the escape will no longer be ignored, you'll get a warning instead. This applies both to re.sub() replacement patterns and to the regular expression patterns. The same warning is placed in the section on regex pattern syntax.

The Changed in version 3.5 line also concerns how re.sub() works. Rather than raise an exception when there is no matching group for a \digit escape sequence, an empty string is inserted at that location.