Cmag Cmag - 2 months ago 24
Python Question

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


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)

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.

>>>'\d\.\d\.\d', 'factor-1.6.2.tar.gz').group(0)