2014-10-10 20 views
0

正則表達式之前,我與一個結構字符串的多個實例:一切後/ Python中

RT @username: Tweet text 

我需要捕捉的用戶名(以後構建的網絡)。 到目前爲止,我有這樣的:

re.findall('\@(.*)') 

應該得到後「@」的一切,但我有一個很難搞清楚如何(不含)之前得到的一切「:」。

回答

5

要獲得@:之間的一切,你可以使用模式:

@([^:]+) 

下面是它匹配什麼故障:

@  # @ 
(  # The start of a capture group 
[^:]+ # One or more characters that are not : 
)  # The close of the capture group 

這裏是一個演示:

>>> from re import findall 
>>> mystr = '''\ 
... RT @username: Tweet text 
... RT @abcde: Tweet text 
... RT @vwxyz: Tweet text 
... ''' 
>>> findall('@([^:]+)', mystr) 
['username', 'abcde', 'vwxyz'] 
>>>