開始使用免費開始

換個名字試試看

你仍在進行 Twitter 情緒分析。現在你要分析一些引起你注意的內容。你發現有些推文中插入了電子郵件地址。你想知道哪個名字最常見。

你想要擷取電子郵件的前半部。例如,若電子郵件是 [email protected],你只關心 marysmith90
你需要比對整個表達式,這樣才能確保只擷取出現在電子郵件中的名稱。而且,你只對包含大寫(例如 A、B、Z)或小寫字母(例如 a、d、z)以及數字的名稱有興趣。

包含三則推文文字的清單 sentiment_analysisre 模組已載入到你的工作階段。你可以在 IPython Shell 中使用 print() 檢視它。

本練習屬於課程

Python 中的正規表示法

檢視課程

練習說明

  • 補上正規表示式,只比對電子郵件中「名稱」那一段。名稱出現在 @ 之前。
  • sentiment_analysis 的每個元素中,找出此正規表示式的所有符合結果。指定給變數 email_matched
  • 補上 .format() 方法,將在 sentiment_analysis 各元素中擷取到的結果列印出來。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Write a regex that matches email
regex_email = r"___[____]____\S+"

for tweet in sentiment_analysis:
    # Find all matches of regex in each tweet
    email_matched = re.____(____, ____)

    # Complete the format method to print the results
    print("Lists of users found in this tweet: {}".format(____))
編輯並執行程式碼