python – Regex for detecting arbitrary alphanumerics (with possible special characters in between) that are *not* purely alphabetical

Let’s say we have a string with free text, most of which is words but some entries which are numbers or serial numbers or anything of the sort:

text == """My name is Maximus Awesomeus and my phone number is +13204919920, my sort code is 01-42-42 and my ID is ZUI8012IOI1. Here is a random string that shouldn't be caught: UHAHS-IQOEQI but here is a random string that should be caught IAIUH124242JOOO-1213IH/131IOIHIO"""

In a Regex search I would like to ignore all words and basically find anything that could be a serial number or number or anything of the sort. In this case that would be:

+13204919920, 01-42-42, ZUI8012IOI1, IAIUH124242JOOO-1213IH/131IOIHIO

I came up with this pattern:

\b(?=.*\d)[A-Za-z0-9._@#/-+]+\b

But the look ahead, looks through the entire string and thus the purely alphabetical words get caught as well if there’s even a single number in the rest of the string. I’m not sure how to get around that – regex has never been a strong suit.

Read more here: Source link