Jul-25-2022, 06:28 AM
When you know what you want to replace it is easy to remove the digits. This uses a comprehension strip the numbers.:
from io import StringIO import re test_text = StringIO( """25 - not this number1 the cow just over the moon and the sun is in 1the sky 26 - not this number 5one day is soon and soon is near take 5529care, 30over and out 59 - not this number The covers at near the back of the 59closet, and when found have them place on the each of the beds. However you see the pillow cases use the ones on the 9second shelve.""" ) pattern = re.compile(r"[0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+") for line in test_text: matches = re.findall(pattern, line) if matches: for match in matches: line = line.replace(match, "".join([c for c in match if c not in '0123456789'])) print(line.rstrip())
Output:25 - not this number
the cow just over the moon and the sun is in the sky
26 - not this number
one day is soon and soon is near take care, over and out
59 - not this number
The covers at near the back of the closet, and when found have them place on the each of the beds. However you see the pillow cases use the ones on the second shelve.
And this uses another regex.for match in matches: line = line.replace(match, re.findall(stripper, match)[0])But it is better to use re.sub(). Write a function that returns a digit-less version of the matching string. This function is the repl argument to the re.sub(patter, repl, string) call.
from io import StringIO import re test_text = StringIO( """25 - not this number1 the cow just over the moon and the sun is in 1the sky 26 - not this number 5one day is soon and soon is near take 5529care, 30over and out 59 - not this number The covers at near the back of the 59closet, and when found have them place on the each of the beds. However you see the pillow cases use the ones on the 9second shelve.""" ) stripper = re.compile(r"[a-zA-Z]+") finder = re.compile(r"[0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+") def strip_digits(match): """This is the repl function used by re.sub()""" return re.findall(stripper, match.group())[0] for line in test_text: print(re.sub(finder, strip_digits, line).rstrip())