简体   繁体   中英

Python regex re.compile() match string

I am trying to grab the version number from a string via python regex...

Given filename: facter-1.6.2.tar.gz

When, inside the loop:

import re
version = re.split('(.*\d\.\d\.\d)',sfile)
print version

How do I get the 1.6.2 bit into version

match = re.search(r'\d\.\d\.\d', sfile)
if match:
    version = match.group()

Two logical problems:

1) Since you want only the 1.6.2 portion, you don't want to capture the .* part before the first \\d , so it goes outside the parentheses.

2) Since you only want to match the pattern in question and grab it, using re. split makes no sense. Instead, use re.match . This will give you a Match object, and you can use its .group() method to get the actual matched text. "group 0" is the entire matched pattern, "group 1" is what's matched by the stuff inside the first set of parentheses, etc.

>>> re.match('.*(\d\.\d\.\d)', 'factor-1.6.2.tar.gz').group(1)
'1.6.2'

Although as the other answer indicates, there's actually no point in matching the .* part anyway, because we can instead search for the string that consists of only the part we want. This will look for the pattern anywhere within the string (matching expects it to be at the beginning). Since we don't need parentheses to make the pattern work logically, and because we're now going to use the entire matched portion, we no longer have any need for parentheses, either.

>>> re.search('\d\.\d\.\d', 'factor-1.6.2.tar.gz').group(0)
'1.6.2'
>>> re.search(r"\d+(\.\d+)+", sfile).group(0)
'1.6.2'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM