Jul-26-2022, 12:54 AM
(Jul-25-2022, 07:32 PM)snippsat Wrote:(Jul-25-2022, 05:15 AM)giddyhead Wrote: Instead of finding the whole thing can it be modified to only find the numbers only for example 5one, 5529care, 30over, etc? ThanksIf add a group()
to deanhystad code then will get only 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: print(f"{line}Matches = {matches}\n") else: print(f"{line}No matches\n")Clean up.
Output:25 - not this number1 Matches = [('', '1')] the cow just over the moon and the sun is in 1the sky Matches = [('1', '')] 26 - not this number No matches 5one day is soon and soon is near take 5529care, 30over and out Matches = [('5', ''), ('5529', ''), ('30', '')] 59 - not this number No matches 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.Matches = [('59', ''), ('9', '')]
>>> Matches = [('5', ''), ('5529', ''), ('30', '')] >>> [i[0] for i in Matches] ['5', '5529', '30']
Got it Thank you!