Extract all hashtags from text
Jump to navigation
Jump to search
使用 Regular expression 擷取文字中的 Hashtag (主題標籤)
資料預先處理[edit]
- 因為連結可能包含 # 符號,所以需要事先移除連結文字。可參考 從文章內容中擷取網址。
擷取文字中的 Hashtag[edit]
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
How to replace all hashtags from text[edit]
To remove all hashtags from a text cell in Google Sheets, use the following formula:
=TRIM(REGEXREPLACE(A1, "\B#[^\s]+", ""))
The following examples illustrate hashtags that will be removed from your text:
#12345 #___ #_ #__123 #1_abc #_abc
References[edit]
延伸閱讀