Message377669
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. |
|
Date |
User |
Action |
Args |
2020-09-29 15:00:38 | dkreeft | set | recipients:
+ dkreeft, paul.moore, tim.golden, ezio.melotti, mrabarnett, zach.ware, steve.dower |
2020-09-29 15:00:37 | dkreeft | set | messageid: <1601391637.99.0.417746893206.issue41885@roundup.psfhosted.org> |
2020-09-29 15:00:37 | dkreeft | link | issue41885 messages |
2020-09-29 15:00:37 | dkreeft | create | |
|