extract a pattern from different string in Python -


i have few file name :

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

here xyz can string of size(only alpha no numerals)

here numbers with('.') version each file.

can have 1 common function extract version each of filename? tried function getting big , use of hard coded constants. please suggest simple way

we can use re module this. let's define pattern we're trying match.

we'll need match string of digits:

\d+ 

these digits may followed either period or hyphen:

\d+[\-\.]? 

and pattern can repeat many times:

(\d[\-\.]?)* 

finally, end @ least 1 digit:

(\d+[\-\.]?)*\d+ 

this pattern can used define function returns version number 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 can use function extract versions data 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) filename in data] 

the result list ask for:

['1.23.35.10.2', '90-12-13-689', '13.23.789.0'] 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -