regex – Python: strip a wildcard word

regex – Python: strip a wildcard word

Use re.sub(pattern, , original_string) to remove matching part from original_string:

>>> import re
>>> string1 = one.two.three.four.five.six.eight
>>> string2 = one.two.hello.four.five.six.seven
>>> re.sub(r^one.two.w+.four, , string1)
.five.six.eight
>>> re.sub(r^one.two.w+.four, , string2)
.five.six.seven

BTW, you are misunderstanding str.lstrip:

>>> abcddcbaabcd.lstrip(abcd)

str.replace is more appropriate (of course, re.sub, too):

>>> abcddcbaabcd.replace(abcd, )
dcba
>>> abcddcbaabcd.replace(abcd, , 1)
dcbaabcd

regex – Python: strip a wildcard word

Leave a Reply

Your email address will not be published. Required fields are marked *