[issue41885] Unexpected behavior re.sub() with raw f-strings

2020-09-29 Thread Eric V. Smith
Change by Eric V. Smith : -- resolution: -> not a bug stage: -> resolved status: open -> closed ___ Python tracker ___ ___ Python-

[issue41885] Unexpected behavior re.sub() with raw f-strings

2020-09-29 Thread Matthew Barnett
Matthew Barnett added the comment: Arguments are evaluated first and then the results are passed to the function. That's true throughout the language. In this instance, you can use \g<1> in the replacement string to refer to group 1: re.sub(r'([a-z]+)', fr"\g<1>{REPLACEMENT}", 'something')

[issue41885] Unexpected behavior re.sub() with raw f-strings

2020-09-29 Thread Eric V. Smith
Eric V. Smith added the comment: f-strings are indeed evaluated when the value of the string is needed. Your example is equivalent to: >>> re.sub(r'([a-z]+)', fr"\112345", 'something') 'J345' As always with regexes, you need to be careful when dynamically composing them. -- nosy: +e

[issue41885] Unexpected behavior re.sub() with raw f-strings

2020-09-29 Thread dkreeft
New submission from dkreeft : 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