如何在分割线上使用反向?

时间:2018-07-14 00:51:23

标签: python regex string

我想解析变量output并打印最新标签(按时间),当前我正在打印最旧的标签,有没有办法将reverse与分割线一起使用?

import re
output="""Fri Apr 27 18:43:20 2018 username3 <username3@company.com> submitted CoreCapture-274.1 to release1
Fri Apr 27 18:43:20 2018 username3 <username3@company.com> forwarded CoreCapture-274.1 to release2, release2E, release1E, release3, and release3E
Fri May 18 20:09:22 2018 username2 <username2@company.com> submitted CoreCapture-274.2 to release1
Fri May 18 20:09:22 2018 username2 <username2@company.com> forwarded CoreCapture-274.2 to release2, release2E, release1E, release3, and release3E
Fri Jun  8 13:35:44 2018 username1 <username1@company.com> submitted CoreCapture-274.3 to release1
Fri Jun  8 13:35:44 2018 username1 <username1@company.com> forwarded CoreCapture-274.3 to release2, release2E, release1E, release3, and release3E"""

train = "release2"
for line in output.splitlines():
    #print "line"
    #print line
    if re.match(r'.*(submitted|forwarded)(.*) to .*%s.*'%train,line):
        print line
        break

输出:-

CoreCapture-274.1

预期的输出:-

CoreCapture-274.3

5 个答案:

答案 0 :(得分:3)

splitlines调用包装在reversed中,它将以相反的顺序迭代结果行(没有其他临时list s):

for line in reversed(output.splitlines()):

或者,如果您需要list本身以相反的顺序,则可以将其两行:

lines = output.splitlines()
lines.reverse()  # Reverses in place

或将其与可逆片式对齐(以额外的临时list为代价):

lines = output.splitlines()[::-1]  # Returns reversed list

答案 1 :(得分:2)

您可以尝试使用:

for line in reversed(output.splitlines()) :

答案 2 :(得分:0)

import re
output="""Fri Apr 27 18:43:20 2018 username3 <username3@company.com> submitted CoreCapture-274.1 to release1
Fri Apr 27 18:43:20 2018 username3 <username3@company.com> forwarded CoreCapture-274.1 to release2, release2E, release1E, release3, and release3E
Fri May 18 20:09:22 2018 username2 <username2@company.com> submitted CoreCapture-274.2 to release1
Fri May 18 20:09:22 2018 username2 <username2@company.com> forwarded CoreCapture-274.2 to release2, release2E, release1E, release3, and release3E
Fri Jun  8 13:35:44 2018 username1 <username1@company.com> submitted CoreCapture-274.3 to release1
Fri Jun  8 13:35:44 2018 username1 <username1@company.com> forwarded CoreCapture-274.3 to release2, release2E, release1E, release3, and release3E"""

train = "release2"
l=output.splitlines()
l.reverse()
for line in l:
    #print "line"
    #print line
    if re.match(r.*(submitted|forwarded)(.*) to .*%s.*'%train,line):
        print line
        break

答案 3 :(得分:0)

要使用正则表达式的简化版本

if train in line:
    match = re.search(r'(submitted|forwarded)\s+(\S+)'
    if match:
        print match.group(2)
        break

在您提交并转发“ word”后,您的版本会打印整行。

答案 4 :(得分:0)

import re

m=re.fullmatch(r"(?ms).*(?:submitted|forwarded)\s+(.*?)\s+to(?!.*CoreCapture).*",output)
print(m.group(1))

Output:
CoreCapture-274.3
#------------------
(?ms)  m:multiline,s: . matches \n, too
(?!.*CoreCapture) negative lookahead to sure no more 'CoreCapture' string in the rest, so the group must match the last "CoreCapture".  

但是它不能处理“ release2”的情况。 增强版:

m=re.fullmatch(r"(?ms).*(?:submitted|forwarded)\s+(.*?)\s+to.*release2(?!.*CoreCapture.*release2).*",output)