Returns the groups from the most recent match/find. If there are no
nested groups, returns a string of the entire match. If there are
nested groups, returns a vector of the groups, the first element
being the entire match.
user=> (def phone-number "672-345-456-3212") #'user/phone-number user=> (def matcher (re-matcher #"((\d+)-(\d+))" phone-number)) #'user/matcher user=> (re-find matcher) ["672-345" "672-345" "672" "345"] ;; re-groups gets the most recent find or matches user=> (re-groups matcher) ["672-345" "672-345" "672" "345"] user=> (re-groups matcher) ["672-345" "672-345" "672" "345"] user=> (re-find matcher) ["456-3212" "456-3212" "456" "3212"] user=> (re-groups matcher) ["456-3212" "456-3212" "456" "3212"] user=> (re-groups matcher) ["456-3212" "456-3212" "456" "3212"] user=> (re-find matcher) nil user=> (re-groups matcher) IllegalStateException No match found java.util.regex.Matcher.group (Matcher.java:468)
(defn re-groups
"Returns the groups from the most recent match/find. If there are no
nested groups, returns a string of the entire match. If there are
nested groups, returns a vector of the groups, the first element
being the entire match."
{:added "1.0"}
[^java.util.regex.Matcher m]
(let [gc (. m (groupCount))]
(if (zero? gc)
(. m (group))
(loop [ret [] c 0]
(if (<= c gc)
(recur (conj ret (. m (group c))) (inc c))
ret)))))
Comments top
No comments for re-groups. Log in to add a comment.