簡體   English   中英

python正則表達式前瞻和否定前瞻

[英]python regex lookahead and negative lookahead

我正在嘗試提取我的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')]

請幫助我實現以上結果。

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']]

現在,您可以輕松地刪除,添加或執行所需的任何操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM