views:

56

answers:

6

Hello,

I have a string which has a version number. I want to read the version number from this code so I can compare it with other code I am using. I have code done below but cannot get it working, can anyone see the problem?

print results

    r = re.compile(r'(version\s*\s*)(\S+)') 

    for l in results: 
        m1 = r.match(l) 
        if m1: 
            ID=map(int,m1.group(2).split("."))  
            l = r.sub(r'\g<1>' + '.'.join(['%s' % (v) for v in ID]), l)
            print ID

the results variable is:

Name Info Type Call version 1.0.40.437 Fri Oct  2 10:54:35 BST 2009

I have it done this way as I need the numbers in the ID separated into groups as I need to compare the 3rd number in the ID to the third number in the ID in another file.

The below answers are useful, but the way I had it would read a file and take all the numbers out and put them into a list so all I would have to do is compare the two numbers of the list. Sorry if the question was not clear but I don't want the version number to be a string.

Okay I made a couple of changes to the code that was answered below. The code is as follows:

    version = re.compile('version\s+([\d.]+)\s+') 
    ID = version.search(results) 
    if ID: 
        value = ID.group(1).split('.')[2]

    self.assertEqual(BUILD_ID[2], int(value))

This does not create the list that I wanted but it allows me to compare the 2 values.

Thanks for all the help.

A: 
>>> import re
>>> results = "Name Info Type Call version 1.0.40.437 Fri Oct  2 10:54:35 BST 2009"
>>> m = re.search("version ([^ ]+)", results)
>>> if m:
...   version = m.group(1)
...   print "matched, found:", version
... else:
...   print "didn't find a version"
... 
matched, found: 1.0.40.437
Roger Pate
+2  A: 

here's a non regex way

>>> s="Name Info Type Call version 1.0.40.437 Fri Oct  2 10:54:35 BST 2009".split()
>>> for n,i in enumerate(s):
...   if "version" in i:
...     print s[n+1]
...
1.0.40.437
>>>
ghostdog74
A: 

I can spot a couple of things:

  • You say results is a string, but you're iterating through it - so l is a character each time.

  • re.match only matches at the beginning of a string. Use re.search instead.

Daniel Roseman
+2  A: 
>>> r = re.compile(r'version (\S*)')
>>> r.findall(results)
['1.0.40.437']

Non regexp way

>>> m=results.split()
>>> m[m.index('version')+1]
'1.0.40.437'
gnibbler
+2  A: 

Why regexp? I should use split(' ') and use value next to 'version', or simplier:

print results.split(' ')[5]

If you must use regexp then try:

rx = re.compile('version\s+([\d.]+)\s+')
rxx = rx.search(results)
if rxx:
    print rxx.group(1)
Michał Niklas
+1 The first way here is best if the format is fixed
gnibbler
`[\d|\.]` doesn't do what you think it does; did you mean `[\d.]`?
Roger Pate
`\d` means digit, and `\.` means dot that separates numbers in version info. I think `\S` would be better if version info can contain info like `7.50a` known from TotalCommander :)
Michał Niklas
@Michał: The dot has no special meaning in a character class. It does not need escaping. Also, a character class always says "one of these", you must not include `|` unless you want to match it.
Tomalak
Thanks. I changed `[\d|\.]` to `[\d.]`
Michał Niklas
A: 

The below answers are useful, but the way I had it would read a file and take all the numbers out and put them into a list so all I would have to do is compare the two numbers of the list.

I'm going to assume that the version format is fixed (ie. version.major.minor.revision).

reVersion = re.compile( 'version\s+((((\d+)\.(\d+))\.(\d+)).(\S+))\s+', re.I )

for result in results:
    versionMatch = reVersion.match( result )
    if versionMatch:
        version = versionMatch.groups()
        print( version[0] ) # 1.0.40.437  full version
        print( version[1] ) # 1.0.40      version.major.minor - no revision
        print( version[2] ) # 1.0         version.major
        print( version[3] ) # 1           version
        print( version[4] ) # 0           major
        print( version[5] ) # 40          minor
        print( version[6] ) # 437         revision
poke