One problem with the proposed solutions is that they do not recognize correctly the quoted parts in the text.
Let us call the quoted parts starting with #
or :
"special" and the rest "non-special".
As an example, in the text "a"#b"c"
, "#b"
is recognized as a special part and "a#bc"
is produced, whereas "a"
and "c"
should be recognized as non-special parts and the text should be left unchanged.
Another problem is that the escaping of "
and \
inside quoted parts is not handled.
One possible solution that takes account of these issues is the following:
(defn remove-quotes [s]
(clojure.string/replace s
#"\"([#:]?)(?:([^\"\\]+)|\\([\"\\]))*\""
#(if (empty? (second %)) (first %) (apply str (rest %)))))
EDIT:
After reading Taylor Wood's answer which treats only a limited case, I decided to add a no-regex solution (which does not handle escaping):
(defn remove-quotes [s]
(loop [processed "" remaining s]
(if-let [i (clojure.string/index-of remaining \u0022)]
(let [j (clojure.string/index-of remaining \u0022 (inc i))]
(recur
(str processed
(subs remaining 0 i)
(apply subs remaining
(if (#{\# \:} (get remaining (inc i)))
[(inc i) j]
[i (inc j)])))
(subs remaining (inc j))))
(str processed remaining))))
\u0022
is just \"
. The latter messes up the code's appearance in Stack Overflow.