Regexs already allow for alternation with the |
operator.
user=> (re-seq #"\d+" "123 foo 345 bar")
("123" "345")
user=> (re-seq #"[a-zA-Z]+" "123 foo 345 bar")
("foo" "bar")
user=> (re-seq #"\d+|[a-zA-Z]+" "123 foo 345 bar")
("123" "foo" "345" "bar")
You can programatically union the regex patterns if desired by interposing the |
operator.
(defn union-re-patterns [& patterns]
(re-pattern (apply str (interpose "|" (map #(str "(?:" % ")") patterns)))))
user=> (union-re-patterns #"\d+" #"[a-zA-Z]+")
#"(\d+)|([a-zA-Z]+)"
user=> (map first (re-seq (union-re-patterns #"\d+" #"[a-zA-Z]+") "123 foo 345 bar"))
("123" "foo" "345" "bar")