Python Regex Sub with Multiple Patterns
问题 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)|' '(