Is it possible to insert different values in a string based on the same matching pattern by using a list to specify the values to insert? I'm using python 3 and re.sub.
Here is what a mean:
line = "Float: __, string: __"
values = ["float", "string"]
newLine = re.sub('(_+)([.]_+)?',
(lambda m:
f'%'
f'{len(m.group(1))}'
f'{"." if values == "float" else ""}'
f'{(len(m.group(2) or ".")-1) if values == "float" else ""}'
f'{"f" if values == "float" else "s"}'),
line)
print(newLine)
Expected result:
Float: %2.0f, string: %2s
Actual result:
Float: %2s, string: %2s
Is it posssible to loop through the values list in order to get the correct result (this is not working and I get a TypeError)? Something like:
newLine = re.sub('(_+)([.]_+)?',
((lambda m:
f'%'
f'{len(m.group(1))}'
f'{"." if v == "float" else ""}'
f'{(len(m.group(2) or ".")-1) if v == "float" else ""}'
f'{"f" if v == "float" else "s"}') for v in values),
line)
Edit 1 The values list and the amount of matching patterns are always of same length.