How do I search and replace a pattern in text using regular expression in Python 3 ?
import re
text = "|105_Oldtext1.1|309_Oldtext1.1|367_Newtext1.6|413_Newtext1.6|"
result = re.sub("105_*", "105_Newtext1.6", text)
print(result)
what I get as result is:
"|105_Newtext1.6Oldtext1.1|309_Oldtext1.1|367_Newtext1.6|413_Newtext1.6|"
I want to replace 105_(whatever text) by 105_Newtext1.6
The
*
isn't a wildcard here ;) You might need this instead:*
means that the previous character is repeated 0 or more times. So,[^|]*
means any character which is not a pipe character being repeated 0 or more times.