Extract all hashtags from text: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
No edit summary
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
使用 [[Regular expression]]  擷取文字中的 Hashtag ( [https://zh.wikipedia.org/wiki/%E4%B8%BB%E9%A1%8C%E6%A8%99%E7%B1%A4 主題標籤])
使用 [[Regular expression]]  擷取文字中的 Hashtag ([https://zh.wikipedia.org/wiki/%E4%B8%BB%E9%A1%8C%E6%A8%99%E7%B1%A4 主題標籤])


== 資料預先處理 ==
== 資料預先處理 ==
* 因為連結包含 # 符號,所以需要事先移除連結文字。
* 因為連結可能包含 # 符號,所以需要事先移除連結文字。可參考 [[Extract url from text | 從文章內容中擷取網址]]。


== 擷取文字中的 Hashtag ==
== 擷取文字中的 Hashtag ==
* 需要處理「[https://zh.wikipedia.org/zh-tw/%E6%A0%87%E7%82%B9%E7%AC%A6%E5%8F%B7 標點符號]與特殊字元(例如 $ 和 %)」<ref>[https://www.facebook.com/help/587836257914341 如何使用主題標籤(Hashtag)? | Facebook 使用說明]</ref>。僅處理部分符號的 PHP code: <ref>[http://stackoverflow.com/questions/3060601/retrieve-all-hashtags-from-a-tweet-in-a-php-function regex - Retrieve all hashtags from a tweet in a PHP function - Stack Overflow]</ref>:
* 需要處理「[https://zh.wikipedia.org/zh-tw/%E6%A0%87%E7%82%B9%E7%AC%A6%E5%8F%B7 標點符號]與特殊字元(例如 $ 和 %)」<ref>[https://www.facebook.com/help/587836257914341 如何使用主題標籤(Hashtag)? | Facebook 使用說明]</ref>。僅處理部分符號的 PHP code 範例: <ref>[http://stackoverflow.com/questions/3060601/retrieve-all-hashtags-from-a-tweet-in-a-php-function regex - Retrieve all hashtags from a tweet in a PHP function - Stack Overflow]</ref>:
<pre>
<pre>
preg_match_all('/\B(#[^\s|\-|-|,|,|\[|\]|『|』|/|~|~|'|"|`|\(|\)|(|)|【|】|《|》|「|」|\<|\>|\=|\{|\}|!|\!|、|?|\?|:|…|\:|;|#|\.|.|。|\$|%|&|\*|\+|,|@|\^|\||\/]+)/ui', $string, $matches);
preg_match_all('/\B(#[^\s|\-|-|,|,|\[|\]|『|』|/|~|~|'|"|`|\(|\)|(|)|【|】|《|》|「|」|\<|\>|\=|\{|\}|!|\!|、|?|\?|:|…|\:|;|#|\.|.|。|\$|%|&|\*|\+|,|@|\^|\||\/]+)/ui', $string, $matches);
Line 16: Line 16:
還需要處理以下狀況
還需要處理以下狀況
<pre>
<pre>
illegal
illegal Hashtag
#12345
#12345
#___
#___
Line 22: Line 22:
#__123
#__123


legal
legal Hashtag
#1_abc
#1_abc
#_abc
#_abc
Line 36: Line 36:
# [http://unicode-table.com/cn/ Unicode®字符百科]
# [http://unicode-table.com/cn/ Unicode®字符百科]


[[Category:Regular expression]] [[Category:Text file processing]] [[Category:Data Science]] [[Category:Search]]
[[Category:Regular expression]] [[Category:String manipulation]] [[Category:Data Science]] [[Category:Search]]

Revision as of 19:35, 9 March 2021

使用 Regular expression 擷取文字中的 Hashtag (主題標籤)

資料預先處理

擷取文字中的 Hashtag

  • 需要處理「標點符號與特殊字元(例如 $ 和 %)」[1]。僅處理部分符號的 PHP code 範例: [2]:
preg_match_all('/\B(#[^\s|\-|-|,|,|\[|\]|『|』|/|~|~|'|"|`|\(|\)|(|)|【|】|《|》|「|」|\<|\>|\=|\{|\}|!|\!|、|?|\?|:|…|\:|;|#|\.|.|。|\$|%|&|\*|\+|,|@|\^|\||\/]+)/ui', $string, $matches);

說明

  • \B 比對非「英文字的邊界」[3][4],避免從 another#bad_hashtag 文字中擷取出 #bad_hashtag。
  • \s 空白字元 (Whitespace character)

還需要處理以下狀況

illegal Hashtag
#12345
#___
#_
#__123

legal Hashtag
#1_abc
#_abc

references

延伸閱讀

  1. 中文輸入法常用標點符號簡表
  2. Unicode 表情圖案(emoji ) + 特殊符號字元一覽表@WFU BLOG
  3. Regex Tutorial - Unicode Characters and Properties
  4. Unicode®字符百科