1.2.0 permalink Arrow_down_16x16
  • (re-gsub regex replacement string)
Replaces all instances of 'pattern' in 'string' with
'replacement'. Like Ruby's 'String#gsub'.

If (ifn? replacment) is true, the replacement is called with the
match.

1 Example top

  • user=> (def examplestr (str "jack and jill" \newline "went up the hill"))
    #'user/examplestr
    
    user=> (println examplestr)
    jack and jill
    went up the hill
    nil
    
    user=> (println (re-gsub #"\n" " " examplestr))
    jack and jill went up the hill
    nil
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/str_utils.clj:53 top

(defn re-gsub 
  "Replaces all instances of 'pattern' in 'string' with
  'replacement'.  Like Ruby's 'String#gsub'.
  
  If (ifn? replacment) is true, the replacement is called with the
  match.
  "
  [^java.util.regex.Pattern regex replacement ^String string]
  (if (ifn? replacement)
    (let [parts (vec (re-partition regex string))]
      (apply str
             (reduce (fn [parts match-idx]
                       (update-in parts [match-idx] replacement))
                     parts (range 1 (count parts) 2))))
    (.. regex (matcher string) (replaceAll replacement))))
Vars in clojure.contrib.str-utils/re-gsub: .. apply count defn fn ifn? let range reduce str update-in
Used in 0 other vars

Comments top

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