What I am trying to do is to get rid of the numbers that are between asterics and keep the other numbers around. I run my regex but it erases everything between asterics.
import re
msg = """
1 x * Build your pizza * ($ 99MXN)
1 x * Baitz Cinnamon 16 pieces * ($ 44.50 MXN)
1 x * Potatoes 220 g * ($ 44.50 MXN) """
re.sub(r'\*.*\*', "", msg)
The expected result I am looking for is:
"""
1 x * Build your pizza * ($ 99MXN)
1 x * Baitz Cinnamon pieces * ($ 44.50 MXN)
1 x * Potatoes g * ($ 44.50 MXN)
"""
CodePudding user response:
You can pass a lambda
to re.sub for repl
and filter out the digits for the substring enclosed inside asterisk:
result = re.sub('\*. \*',
lambda x: ''.join(c for c in x.group(0) if not c.isdigit()),
msg)
print(result)
1 x * Build your pizza * ($ 99MXN)
1 x * Baitz Cinnamon pieces * ($ 44.50 MXN)
1 x * Potatoes g * ($ 44.50 MXN)
You can use nested re.sub
if you don't want to use above method (which doesn't remove preceding/following white space characters):
result = re.sub('\*. \*',
lambda x: re.sub('\s*\d \s*','',x.group(0)),
msg)
print(result)
1 x * Build your pizza * ($ 99MXN)
1 x * Baitz Cinnamonpieces * ($ 44.50 MXN)
1 x * Potatoesg * ($ 44.50 MXN)