Conditional Replacement

 
import re

s = "A: 0.75, B: 0.5, C: 0.25, D: 0"

def float2percent(match):
    return f"{match.group(1)}{100*float(match.group(2))}%"

rst = re.sub(r"(\w: )([0-9.]*)", float2percent, s)
rst
'A: 75.0%, B: 50.0%, C: 25.0%, D: 0.0%'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
p = "\(?(\-?[\d]+)\)?\*\(?(\-?[\d]+)\)?"

s   = "100*200+300"
rst = re.search(p, s)
print("\ns:", s)
print("group():", rst.group())
print("group(1):", rst.group(1))
print("group(2):", rst.group(2))

s   = "100*(-200)-300"
rst = re.search(p, s)
print("\ns:", s)
print("group():", rst.group())
print("group(1):", rst.group(1))
print("group(2):", rst.group(2))

s   = "-100*(-200)/300"
rst = re.search(p, s)
print("\ns:", s)
print("group():", rst.group())
print("group(1):", rst.group(1))
print("group(2):", rst.group(2))

s   = "(-100)*(-200)%300"
rst = re.search(p, s)
print("\ns:", s)
print("group():", rst.group())
print("group(1):", rst.group(1))
print("group(2):", rst.group(2))
s: 100*200+300
group(): 100*200
group(1): 100
group(2): 200

s: 100*(-200)-300
group(): 100*(-200)
group(1): 100
group(2): -200

s: -100*(-200)/300
group(): -100*(-200)
group(1): -100
group(2): -200

s: (-100)*(-200)%300
group(): (-100)*(-200)
group(1): -100
group(2): -200