Python中正则表达式之后/之前的所有内容

Python中正则表达式之后/之前的所有内容,python,regex,Python,Regex,我有多个具有下一个结构的字符串实例: RT @username: Tweet text 我需要捕获用户名,以便以后构建网络。 到目前为止,我有: re.findall('\@(.*)') 它应该在“@”之后获取所有内容,但我很难在排除“:”之前找到获取所有内容的方法。要获取@和之间的所有内容,可以使用以下模式: @([^:]+) 以下是匹配项的细分: @ # @ ( # The start of a capture group [^:]+ # One or more

我有多个具有下一个结构的字符串实例:

RT @username: Tweet text
我需要捕获用户名,以便以后构建网络。 到目前为止,我有:

re.findall('\@(.*)') 
它应该在“@”之后获取所有内容,但我很难在排除“:”之前找到获取所有内容的方法。

要获取@和之间的所有内容,可以使用以下模式:

@([^:]+)
以下是匹配项的细分:

@      # @
(      # 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']
>>>
要获取@和:之间的所有内容,可以使用以下模式:

@([^:]+)
以下是匹配项的细分:

@      # @
(      # 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']
>>>