This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dkreeft
Recipients dkreeft, ezio.melotti, mrabarnett, paul.moore, steve.dower, tim.golden, zach.ware
Date 2020-09-29.15:00:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1601391637.99.0.417746893206.issue41885@roundup.psfhosted.org>
In-reply-to
Content
Steps to reproduce (Windows/Python 3.7.7):

1. Define replacement string that starts with an integer:

REPLACEMENT = '12345'

2. Use re.sub() as follows:

re.sub(r'([a-z]+)', fr"\1{REPLACEMENT}", 'something')

3. The outcome is not 'something12345' as expected, but 'J345'.

Note that I am using the group in the replacement argument, which is a raw f-string.

A quick investigation with other replacement strings renders similar unexpected behavior:

REPLACEMENT = '1': leads to re.error (invalid group reference 11 at position 1)
REPLACEMENT = '13': 'K'
etc.

So it seems like the f-string is evaluated first, yielding a string starting with an integer. Python then interprets the '\1' to indicate group 1 as '\1<first integer>', which leads to the behavior described above. Even if this is by design, it seems confusing and makes using groups with re.sub() cumbersome if the replacement f-string starts with an integer.
History
Date User Action Args
2020-09-29 15:00:38dkreeftsetrecipients: + dkreeft, paul.moore, tim.golden, ezio.melotti, mrabarnett, zach.ware, steve.dower
2020-09-29 15:00:37dkreeftsetmessageid: <1601391637.99.0.417746893206.issue41885@roundup.psfhosted.org>
2020-09-29 15:00:37dkreeftlinkissue41885 messages
2020-09-29 15:00:37dkreeftcreate