I want to replace a string contains "${}" with "*" in python.
example, replace:
"This is ${ali} from team"
with
"This is * from team"
I’ve tried this but does not work:
re.sub("^$.*}$", "*",str)
>Solution :
import re
s = "This is ${ali} from team ${asda} "
s = re.sub(r'\${.[^}]+}','*',s)
print(s)
OUTPUT
This is * from team *
Pythonic way
s = "This is ${ali} from team ${asd}"
s = list(s)
try:
while '$' in s and '}' in s:
s[s.index('$'):s.index('}')+1] = '*'
except ValueError:
pass
s = ''.join(s)
print(s) # → This is * from team *