[英]python regex to select everything before and after a particular string
[英]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.