我正在尝试在下面的列表中grep“包含在TEAM_COM_BUILD中”并获取COM_CNSS_BT_LNX.LA.3.6.1.00.00.032的版本,在这种情况下为“32”,预期输出为32但是遇到以下错误?解决它?
import re
comments = [{u'timestamp': 1403046914, u'message': u'Patch Set 1: Looks good to me, but someone else must approve\n\nThis patchset has been processed by the service.', u'reviewer': {u'username': u'service', u'name': u'service Service Account', u'email': u'service@localhost'}}, {u'timestamp': 1403051700, u'message': u'Patch Set 1: Developer Build and Test COMccessful\n\nINCLUDED IN TEAM_COM_BUILD: COM_CNSS_BT_LNX.LA.3.6.1.00.00.032\n\nhttp://qwiki.company.com/div_wcnss/COM_CNSS_BT_LNX.LA.3.6.1.00.00.032', u'reviewer': {u'username': u'user2', u'name': u'USER Prakash Soy', u'email': u'user2@div.company.com'}}, {u'timestamp': 1403052176, u'message': u'Patch Set 1: Looks good to me, approved\n\n', u'reviewer': {u'username': u'username', u'name': u'Alekhya Damera', u'email': u'username@div.company.com'}}]
matchobj = re.search("INCLUDED IN TEAM_COM_BUILD: (\s*)$", str(comments), re.MULTILINE)
print matchobj
build = matchobj.group(1)
print build
chunks = build.split('.')
print chunks
last_one = chunks[-1]
print last_one
错误: -
Traceback (most recent call last):
File "su_version.py", line 5, in <module>
build = matchobj.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
答案 0 :(得分:2)
你的正则表达式错了。这个可行:
matchobj = re.search(r"INCLUDED IN TEAM_COM_BUILD: \S+\.(\d+)\\n", str(comments))
它将在INCLUDED IN TEAM_COM_BUILD
之后贪婪地匹配非空白字符,直到它到达.
字符,然后是一个或多个数字(在匹配组中捕获),然后是换行符。这就是你想要的。
修改强>
在回复您的评论时,您可以使用它来匹配这两种模式:
matchobj = re.search(r"INCLUDED IN TEAM_COM_BUILD:\s+\S+?\.(?:\d+\.){5}(\d+)(?:\.\d+)?", str(comments))
这与第一个实例匹配(它返回第一个实例,因为我们非常贪婪地使用\S+
匹配5个连续\S+?
组,后跟一个或多个数字(这是在一个组中捕获的,可选后跟<one or more digit>.
。括号内的.<one or more digits>
个字符表示该组是非捕获的,括号后的?:
表示该组是可选的。