获取各种输入的版本标记

时间:2014-06-19 01:12:00

标签: python

INPUT: -

tag#1INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.1.00.00.042.1
tag#2INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.00.00.042.1
tag#3INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.00.042.1
tag#4INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.1.c2.00.00.042.1
tag#5INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.1.c2.00.00.042

代码: -

您好,

我有以下代码适用于上面显示的INPUT中的标记#1但是对于其他标记失败,所有输入的输出应该是“042”..有人建议如何解决这个问题吗?

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 Gator.', u'reviewer': {u'username': u'gator', u'name': u'Gator Service Account', u'email': u'gator@localhost'}}, {u'timestamp': 1403051700, u'message': u'Patch Set 1: Developer Build and Test Successful\n\nINCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.1.00.00.042.1\n\nhttp://qwiki.company.com/div_wcnss/SU_TAG_PL_DATA.LA.3.6.1.c2.00.00.042.1', u'reviewer': {u'username': u'username', u'name': u'user name2 ', u'email': u'username@div.company.com'}}, {u'timestamp': 1403052176, u'message': u'Patch Set 1: Looks good to me, approved\n\n', u'reviewer': {u'username': u'username2', u'name': u'user full name', u'email': u'username2@div.company.com'}}]

matchobj = re.search(r"INCLUDED IN DATABUILD:\s+\S+?\.(?:\d+\.){5}(\d+)(?:\.\d+)?", str(comments))
print matchobj

build = matchobj.group(1)
print build

chunks = build.split('.')
print chunks

last_one = chunks[-1]
print last_one

1 个答案:

答案 0 :(得分:0)

如果您在INCLUDE之后寻找前3位数字,那么您可以使用

import re

examples = [
    'tag#1INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.1.00.00.041.1 - next 3-digits numer 007',
    'tag#2INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.00.00.052.1 - next 3-digits numer 007',
    'tag#3INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.00.042.1 - next 3-digits numer 007',
    'tag#4INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.1.c2.00.00.142.1 - next 3-digits numer 007',
    'tag#5INCLUDED IN DATABUILD: SU_TAG_PL_DATA.LA.3.6.1.c2.00.00.002 - next 3-digits numer 007'
]

for x in examples:
    r = re.search(r'INCLUDED IN DATABUILD.*?(\d{3})', x)
    print( r.group(1) )

在示例中,我将042替换为不同的数字,以便更好地查看结果。

041
052
042
142
002