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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.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.