如何在python中搜索给定文本的版本号:

时间:2015-03-19 13:22:53

标签: python regex

如何搜索版本没有python:

Build Host   : iox-lux-013 
Version      : 3.2.8.08I sample

我试过了:

import re
p=re.compile("Version.*: (.*?) ")
list=p.findall(s)
for i in list:
        print i

效果很好 但不适用于:

Version      : 3.2.8.08I  

不起作用:/

2 个答案:

答案 0 :(得分:1)

使用\S+匹配一个或多个非空格字符。

p = re.compile(r"Version\s*:\s*(\d+(?:\.\d+)*[A-Z]\b)")

\s*匹配零个或多个空格,因此您不需要在.*之前使用:

示例:

>>> s = '''Build Host   : iox-lux-013 
Version      : 3.2.8.08I sample
Version      : 3.2.8.08I'''
re.findall(r"Version\s*:\s*(\d+(?:\.\d+)*[A-Z]\b)", s)
['3.2.8.08I', '3.2.8.08I']

答案 1 :(得分:0)

import re

s = '''Build Host   : iox-lux-013
Version      : 3.2.8.08I sample
Version      : 3.2.8.08I '''

 f =  re.findall(r"Version\s+:( [0-9]+.[0-9]+.[0-9]+.[0-9]+[A-Z])(\s[A-Z])*", s)

print(f)

使用\ S +匹配一个或多个非空格字符。

( [0-9]+.[0-9]+.[0-9]+.[0-9]+[A-Z]) 该组将检查版本号。 (\s[A-Z])*将检查字符串+空格是否可用;是否为

* - > 0或更多