调整正则表达式以匹配消息开头的* only *字符串

时间:2018-09-05 23:28:35

标签: python regex python-2.x

我有以下代码与commit_msg中的rdar://problem(一个或多个)匹配,我只想在消息的开头进行匹配,请注意,开头可能不止一个rdar消息中,如何更改正则表达式来做到这一点?

# -*- coding: utf-8 -*-
import re
commit_msg = """
<rdar://problem/19391231> This is the subject line1
<rdar://problem/11121314> This is the subject line2
[Problem]
The Problem description

[Solution]
This is the Solutions section

[Recommended Tests]
This is the Recommended Tests <rdar://problem/12345678> Text

Change-Id: Ibbafa780adb2502d470f12d0280ddb0049c727c4
Reviewed-on: https://tech-gerrit.sd.company.com/17954
Tested-by: Username1 <username1@company.com>
Build-watchOS: service account <serviceaccount@company.com>
Reviewed-by: username2 <username2@company.com>
"""
m = re.findall("(?!.*(?:Revert|revert))[\S]*(?:rdar:\/\/problem\/)(\d{8,8})", commit_msg)

print m

当前输出:-

['19391231', '11121314', '12345678']

预期的输出:-

['19391231', '11121314']

1 个答案:

答案 0 :(得分:1)

在下面与@ShadowRanger进行对话时,如何处理?

import re
commit_msg = """
<rdar://problem/19391231> This is the subject line1
<rdar://problem/11121314> This is the subject line2
[Problem]
The Problem description

[Solution]
This is the Solutions section

[Recommended Tests]
This is the Recommended Tests <rdar://problem/12345678> Text

Change-Id: Ibbafa780adb2502d470f12d0280ddb0049c727c4
Reviewed-on: https://tech-gerrit.sd.company.com/17954
Tested-by: Username1 <username1@company.com>
Build-watchOS: service account <serviceaccount@company.com>
Reviewed-by: username2 <username2@company.com>
"""
m = re.findall("(?!.*(?:Revert|revert))[\S]*(?:rdar:\/\/problem\/)(\d{8,8})", commit_msg.split('[')[0])

print m