Skip to content

Instantly share code, notes, and snippets.

@adamneilson
Last active December 17, 2015 22:39
Show Gist options
  • Save adamneilson/5684065 to your computer and use it in GitHub Desktop.
Save adamneilson/5684065 to your computer and use it in GitHub Desktop.
Sample code to scan a body of text for @mentions and #tags
;=============================
; a sample body of text
;=============================
(def sample "A mention: @bob and another: \uFF20charlie but not me@home.com and #my_tag should be caught as should #another. Tests passed? #blimey!")
;=============================
; valid at chars are @ and \uFF20
;=============================
(def mention-pattern #"(^|\s)[@\uFF20](\w+)")
(def tag-pattern #"(^|\s)#(\w+)")
(defn extract-group [n] (fn [group] (group n)))
;=============================
; scan comment for mentions
;=============================
(defn scan-for-mentions [comment]
(let [matches (re-seq mention-pattern comment)]
(map (extract-group 2) matches)))
;=============================
; scan comment for hash tags
;=============================
(defn scan-for-tags [comment]
(let [matches (re-seq tag-pattern comment)]
(map (extract-group 2) matches)))
;=============================
; Test the sample
;=============================
(scan-for-mentions sample)
(scan-for-tags sample)
; This renders:
;("bob" "charlie")
; and
;("my_tag" "another" "blimey")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment