python - how to get the version number of a tag in a list -
i trying grep "included in team_com_build" in list below , version of com_cnss_bt_lnx.la.3.6.1.00.00.032,"32" in case,expected output 32 running below error?how prepare it?
import re comments = [{u'timestamp': 1403046914, u'message': u'patch set 1: looks me, else must approve\n\nthis patchset has been processed 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 , 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 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
error:-
traceback (most recent phone call last): file "su_version.py", line 5, in <module> build = matchobj.group(1) attributeerror: 'nonetype' object has no attribute 'group'
your regular look wrong. 1 work:
matchobj = re.search(r"included in team_com_build: \s+\.(\d+)\\n", str(comments))
it greedily match non-whitespace characters after included in team_com_build
until hits .
character, followed 1 or more digits (which captured in match group), followed newline. want.
edit:
in response comment, can utilize match both patterns:
matchobj = re.search(r"included in team_com_build:\s+\s+?\.(?:\d+\.){5}(\d+)(?:\.\d+)?", str(comments))
this matches first instance (it returns first instance because match \s+
non-greedily, using \s+?
) of 5 consecutive <one or more digit>.
groups, followed 1 or more digits (which captured in group), optionally followed .<one or more digits>
. ?:
characters within of parenthesis means grouping non-capturing, , ?
after parenthesis marks grouping optional.
python
No comments:
Post a Comment