I'm trying to match multiple patterns using regex sub grouping and replace the match with an asterisk for a data file that has similar format to the string below. However, I am getting only the desired results for the first match. The subsequent matches are consuming string that I did not expect. Is there a better approach to getting the desired output below?
import re
myString = '-fruit apple -number 123 -animal cat -name bob'
match = re.compile('(-fruit\s+)(\w+)|'
'(-animal\s+)(cat)|'
'(-name\s+)(bob)')
print(match.sub('\g<1>*', myString))
Current Output:
-fruit * -number 123 * *
Desired Output:
-fruit * -number 123 -animal * -name *