1.3.0 permalink Arrow_down_16x16

re-groups

clojure.core

  • (re-groups m)
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.

1 Example top

  • 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)
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:4264 top

(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"
   :static true}
  [^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)))))
Vars in clojure.core/re-groups:
Used in 0 other vars

Comments top

No comments for re-groups. Log in to add a comment.