Jul-25-2022, 04:26 AM
Looks like you want to match one or more numbers followed by one or more letters. I also decided to catch one or more letters followed by one or more numbers
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")
Output:25 - not this number1
Matches = ['number1']
the cow just over the moon and the sun is in 1the sky
Matches = ['1the']
26 - not this number
No matches
5one day is soon and soon is near take 5529care, 30over and out
Matches = ['5one', '5529care', '30over']
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 = ['59closet', '9second']