Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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-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 ?

like image 398
Zeljko Avatar asked Mar 31 '10 14:03

Zeljko


People also ask

How do I remove a word from a list of strings in Python?

We can use the replace() function to remove word from string in Python. This function replaces a given substring with the mentioned substring. We can replace a word with an empty character to remove it. We can also specify how many occurrences of a word we want to replace in the function.

How do I remove a string from a list?

Use the list. pop() method to remove a string from a list, e.g. my_list. pop(0) .

How do I remove an item from a string in Python?

Using replace(): Replace is one of the most common methods used to remove a character from a string in Python. Although, replace() was intended to replace a new character with an older character - using "" as the new character can be used to remove a character from a string.


2 Answers

(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))
like image 153
cgrand Avatar answered Sep 29 '22 23:09

cgrand


(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))
like image 28
Jouni K. Seppänen Avatar answered Sep 29 '22 23:09

Jouni K. Seppänen