2014-10-01 72 views
0

我有文字的一些作品是这样的:Python的正则表达式捕获文“:”和“”(点后面的空格)

GAEDS030, GAEDS031, GAEDS032 : Problem reported in a https://twikiae.myweb.es 
/twiki/bin/view/Grid/ActFeb2011 previous entry has been observed in another disk server 
that was under a stress test (gaeds034). We have contacted technical service at LSI and 
they have suggested us to update to a brand new firmware (!FE9X 4.10.00.021). After a new 
test period if seems that raid card does not reset under heavy load as in the past. So it 
has been upgraded every card of this branch (gaeds030-gaeds034) 

他们中有些人没有冒号。

现在,我使用这个正则表达式后第一:和前第一.

re.search(':([^\.]*)(\.)*', description) 

,这捕获文本时它没有:

re.search('((.*)(?!\.))', description) 

正如你所看到的,我有URL,IP等问题,所以我想在:之后的.(点后跟空格)之前捕获文本。

我试过白色否定组,但它不允许将它们用于组。

+4

您的预期产出是? – 2014-10-01 07:07:45

+0

实际输出为:'在https:// twikiae中报告的问题,但我希望获得'在https://twikiae.myweb.es /twiki/bin/view/Grid/ActFeb2011之前报告的问题在另一台正在压力测试(gaeds034)下的磁盘服务器中观察到入口 – 2014-10-01 07:12:17

回答

0

您可以使用:

:\s*(.*?)\.\s 

这里有一个demo

0

由于输入中存在换行符,因此最好使用DOTALL modifier (?s)来使换行符也匹配换行符。所以这场比赛将在多条线上进行。

(?<=:\s).*?(?=\.\s) 

DEMO

>>> s = """GAEDS030, GAEDS031, GAEDS032 : Problem reported in a https://twikiae.myweb.es 
... /twiki/bin/view/Grid/ActFeb2011 previous entry has been observed in another disk server 
... that was under a stress test (gaeds034). We have contacted technical service at LSI and 
... they have suggested us to update to a brand new firmware (!FE9X 4.10.00.021). After a new 
... test period if seems that raid card does not reset under heavy load as in the past. So it 
... has been upgraded every card of this branch (gaeds030-gaeds034)""" 
>>> re.search(r'(?s)(?<=:\s).*?(?=\.\s)', s).group() 
'Problem reported in a https://twikiae.myweb.es\n/twiki/bin/view/Grid/ActFeb2011 previous entry has been observed in another disk server\nthat was under a stress test (gaeds034)' 
>>> m = re.search(r'(?s)(?<=:\s).*?(?=\.\s)', s).group() 
>>> print m 
Problem reported in a https://twikiae.myweb.es 
/twiki/bin/view/Grid/ActFeb2011 previous entry has been observed in another disk server 
that was under a stress test (gaeds034)