4

I'm new in clojure programming. I'm learning about splitting a string by defining regular expressions. I'm learning from here https://clojuredocs.org/clojure.string/split

I want to split a string by defining two regular expressions. For example:

=> (require '[clojure.string :as str])

=> (str/split "Hello world! Have a nice day" #" ")
;; ["Hello" "world!" "Have" "a" "nice" "day"]

=> (str/split "Hello world!\nHave a nice day" #"\n")
;; ["Hello world!" "Have a nice day"]

This is cool. Now I'd like to split a string on every space & newline.

If the input is "Hello world!\nHave a nice day", The output should be ["Hello" "world!" "Have" "a" "nice" "day"]

can anyone suggest me, How can I do this? Thanks.

1 Answer 1

5

i would recommend you to use #"\s+" as the splitting regex, because \s symbol class includes all the whitespace characters (as of java regex they are [ \t\n\x0B\f\r]. (https://docs.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html).

user> (clojure.string/split "Hello world! Have  a nice   day\naaa bbb" #"\s+")
["Hello" "world!" "Have" "a" "nice" "day" "aaa" "bbb"]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I got my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.