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.
(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))))
Comments top
No comments for re-sub. Log in to add a comment.