我正在使用python從tweet文本中提取提及的代碼。如何使用python來提取提及?
該參數是一條推文文本。這個函數應該按照它們在推文中出現的順序返回一個包含推文中所有提及的列表。在返回的列表中的每個值得一提的有初始提取出符號和列表應包含遇到的每一個提 - 包括重複,如果用戶不是tweet.Here中曾經提到更多的是兩個例子:
>>>extract_mentions('@AndreaTantaros- You are a true journalistic\
professional. I so agree with what you say. Keep up the great\
[email protected] ')
['AndreaTantaros','RepJohnLewis']
>>>extract_mentions('@CPAC For all the closet #libertarians attending \
#CPAC2016 , I'll be there Thurs/Fri -- speaking Thurs. a.m. on the main\
stage. Look me up! @CPAC')
['CPAC','CPAC']
一個提到以'@'符號開始,幷包含所有字母數字字符,直到(但不包括)空格字符,標點符號或推文結束。
如何從字符串中提取提及的內容?抱歉,我還沒有學過正則表達式,有沒有其他方法?
如果某人的電子郵件地址是 - [email protected]? –
這取決於,你可以簡單地將一個正則表達式匹配一個'.'後的三個字符,就像這樣:'[\ w] + @ [\ w] + \。[a-z] {3}'。 OP沒有提到她想要什麼。 @WasiAhmad – Jarvis
如果我的電子郵件地址是「hello @ example.ninja」,該怎麼辦?或'hello @ example.nl'?或'hello.there @ example.com'?或'hello + there @ example.com'? – Carpetsmoker