How to match info from a string
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 = 开发者_C百科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.
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)
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
>>>
>>> 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'
I can spot a couple of things:
You say
results
is a string, but you're iterating through it - sol
is a character each time.re.match
only matches at the beginning of a string. Usere.search
instead.
>>> 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
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
精彩评论