我有多个具有下一个结构的字符串实例:
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']
>>>
