Search code examples
clojure

Clojure: How to use re-seq to return a list of only alphanumeric chars as individual strings


I am trying to parse user input for a Clojure game. I would like to take any user-input string and turn each alphanumeric character into its own string, and then return a list of all these strings, ignoring whitespace and delimiters. The following illustrates what I am trying to accomplish:

(characters-as-strings "a   b")
; => ("a" "b")

(characters-as-strings "a   c,b")
; => ("a" "c" "b")

I have been trying (defn characters-as-strings [userInput] re-seq #"[A-Z][a-z]" userInput), but this is only returning the actual string "a b". What is a better way to do this?


Solution

  • while the accepted answer solves the subclass of the problem, still it only handles latin letters, while the complete solution for handling alphanumerics including unicode letters could look something like this:

    user> (re-seq #"\p{IsAlphabetic}|[0-9]" "a --- , ..b c ,de 1 22 ฆ й ζ")
    ;;=> ("a" "b" "c" "d" "e" "1" "2" "2" "ฆ" "й" "ζ")