繁体   English   中英

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

[英]Everything after/before regex in Python

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

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']
>>>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM