How to break a line with multiple regexp definition in clojure?

I am new to clojure programming . I learn about line splitting by defining regular expressions. I study here https://clojuredocs.org/clojure.string/split

I want to break a string by specifying two regular expressions. For instance:

=> (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"]

That's cool. Now I would like to split the line on each space and new line .

If the input is "Hello world! \ NChoose a good day . " The result should be ["Hello" "world!" "Have" "good" "day"]

Can anyone suggest me how I can do this? Thank you

+4
source share
1 answer

I would recommend using it #"\s+"as a relational splitting, because \sthe character class includes all whitespace characters (with 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"]
+5
source

Source: https://habr.com/ru/post/1650505/


All Articles