开发者

How to remove list of words from strings

What I would like to do (in Clojure):

For example, I have a vector of words that need to be removed:

(def forbidden开发者_C百科-words [":)" "the" "." "," " " ...many more...])

... and a vector of strings:

(def strings ["the movie list" "this.is.a.string" "haha :)" ...many more...])

So, each forbidden word should be removed from each string, and the result, in this case, would be: ["movie list" "thisisastring" "haha"].

How to do this ?


(def forbidden-words [":)" "the" "." ","])
(def strings ["the movie list" "this.is.a.string" "haha :)"])
(let [pattern (->> forbidden-words (map #(java.util.regex.Pattern/quote %)) 
                (interpose \|)  (apply str))]
  (map #(.replaceAll % pattern "") strings))


(use 'clojure.contrib.str-utils)
(import 'java.util.regex.Pattern)
(def forbidden-words [":)" "the" "." "," " "])
(def strings ["the movie list" "this.is.a.string" "haha :)"])
(def regexes (map #(Pattern/compile % Pattern/LITERAL) forbidden-words))
(for [s strings] (reduce #(re-gsub %2 "" %1) s regexes))


Using function composition and the -> macro this can be nice and simple:

(for [s strings] 
  (-> s ((apply comp 
           (for [s forbidden-words] #(.replace %1 s ""))))))

If you want to be more 'idiomatic', you can use replace-str from clojure.contrib.string, instead of #(.replace %1 s "").

No need to use regexs here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜