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

Tune a Regex to match a string *only* at the beginning of a message

我有以下代码匹配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']

结束你与@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