Мы можем попробовать использовать re.sub
здесь:
inp = "Here is a value $12345.67890 for replacement."
out = re.sub(r'\$(\d+(?:\.\d{1,2})?)\d*\b', '\\1', inp)
print(out)
Это печатает:
Here is a value 12345.67 for replacement.
Вот объяснение шаблона регулярного выражения:
\$ match $
( capture what follows
\d+ match one or more whole number digits
(?:\.\d{1,2})? then match an optional decimal component, with up to 2 digits
) close capture group (the output number you want)
\d* consume any remaining decimal digits, to remove them
\b until hitting a word boundary