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 ?
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.
Use the list. pop() method to remove a string from a list, e.g. my_list. pop(0) .
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.
(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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With