We can use the re module to do this. Let's define the pattern we're trying to match.
We'll need to match a string of digits:
\d+
These digits may be followed by either a period or a hyphen:
\d+[\-\.]?
And this pattern can repeat many times:
(\d[\-\.]?)*
Finally, we always end with at least one digit:
(\d+[\-\.]?)*\d+
This pattern can be used to define a function that returns a version number from a filename:
import re
def version_from(filename, pattern=r'(\d+[\-\.]?)*\d+'):
match = re.search(pattern, filename)
if match:
return match.group(0)
else:
return None
Now we can use the function to extract all the versions from the data you provided:
data = ['xyz-1.23.35.10.2.rpm', 'xyz-linux-version-90-12-13-689.tar.gz', 'xyz-xyz-xyz-13.23.789.0-xyz-xyz.rpm']
versions = [version_from(filename) for filename in data]
The result is the list you ask for:
['1.23.35.10.2', '90-12-13-689', '13.23.789.0']