1.2.0 permalink Arrow_down_16x16
  • (re-sub regex replacement string)
Replaces the first instance of 'pattern' in 'string' with
'replacement'. Like Ruby's 'String#sub'.

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

0 Examples top

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:69 top

(defn re-sub
  "Replaces the first instance of 'pattern' in 'string' with
  'replacement'.  Like Ruby's 'String#sub'.
  
  If (ifn? replacement) is true, the replacement is called with
  the match.
  "
  [^Pattern regex replacement ^String string]
  (if (ifn? replacement)
    (let [m (re-matcher regex string)]
      (if (.find m)
        (str (.subSequence string 0 (.start m))
             (replacement (re-groups m))
             (.subSequence string (.end m) (.length string)))
        string))
    (.. regex (matcher string) (replaceFirst replacement))))
Vars in clojure.contrib.str-utils/re-sub: .. defn ifn? let re-groups str
Used in 0 other vars

Comments top

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