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

时间:2014-10-10 18:53:54

标签: python regex

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

RT @username: Tweet text

我需要捕获用户名(以后构建一个网络)。 到目前为止,我有这个:

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

应该在' @'之后得到所有内容,但我很难弄清楚如何获得所有内容(不包括)':'。'。 p>

1 个答案:

答案 0 :(得分:6)

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

@([^:]+)

以下是匹配内容的细分:

@      # @
(      # 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']
>>>
相关问题