python regex lookahead和negative lookahead

时间:2015-07-17 09:36:00

标签: python regex regex-negation

我正在尝试在我的git commit消息上提取消息。

使用以下代码:

import sys, os, re
alpsym = '[A-Za-z_-]'
num = '[0-9]'
ticket = '(?:ticket|issue|bug[: ]?)'
actions = '(?:re|close|closes|closed|fix|fixes|fixed?)'
msg = "testing re #119 , close ticket:#120, fixed mygroup:#119, close #132, fixes mytools:#131"
result = r'(?p<a>(?:%s*)).?(?p<b>(?!%s)|(?:%s*)).?(?p<c>(?:#|%s)%s+).?' % (actions,actions,alpsym,ticket,num)

结果:

[('', 're', '#119'),
('close', 'ticket', '#120'),
('fixed', 'mygroup', '#119'),
('', 'close', '#132'),
('fixes', 'mytools', '#131')]

但我希望结果是:

[('re', '', '#119'),
('close', '', '#120'),
('fixed', 'mygroup', '#119'),
('close', '', '#132'),
('fixes', 'mytools', '#131')]

请帮助我实现上述结果。

1 个答案:

答案 0 :(得分:1)

x="testing re #119 , close ticket:#120, fixed mygroup:#119, close #132, fixes mytools:#131"
k=re.split("\s*,\s*",x)
print [re.split("\s+|:",i) for i in k]

split然后再玩它会容易得多。

输出:[['testing', 're', '#119'], ['close', 'ticket', '#120'], ['fixed', 'mygroup', '#119'], ['close', '#132'], ['fixes', 'mytools', '#131']]

现在,您可以轻松删除,添加或执行任何操作。