Replaces the first instance of match with replacement in s.
match/replacement can be:
char / char
string / string
pattern / (string or function of match).
See also replace-all.
user=> (use '[clojure.string :only (replace-first)]) ;; Only replace the first match. user=> (replace-first "A good day to you, sir. Good day." #"day" "night") "A good night to you, sir. Good day." ;; If there are no matches, return the original string. user=> (replace-first "A good day to you, sir." #"madam" "master") "A good day to you, sir." ;; (?i) at the beginning of a pattern makes the entire thing match ;; case-insensitively, at least for US ASCII characters. (?u) does ;; the corresponding thing for Unicode. user=> (replace-first "Day need not be SHOUTED." #"(?i)day" "night") "night need not be SHOUTED." ;; See here for many details on regex patterns: ;; http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html ;; Also the book "Mastering Regular Expressions" by Jeffrey Friedl.
user=> (use '[clojure.string :only (replace-first)]) ;; Pattern matches from beginning of string (signified by ^) up to the ;; last occurrence of /, because by default * is greedy, i.e. it ;; matches as much as possible. user=> (replace-first "/path/to/file/name" #"^.*/" "") "name" ;; Use *? to match as little as possible. user=> (replace-first "/path/to/file/name" #"^.*?/" "") "path/to/file/name"
;; Note: When replace-first or replace have a regex pattern as their
;; match argument, dollar sign ($) and backslash (\) characters in
;; the replacement string are treated specially.
;; Example: pattern matches string "fodder", with (o+) matching "o"
;; and (\S+) matching "dder". Replacement string says to replace the
;; entire match "fodder" with $2, the string matched by the second
;; parenthesized group, "dder", followed by $1, "o".
user=> (str/replace-first "fabulous fodder foo food" #"f(o+)(\S+)" "$2$1")
"fabulous ddero foo food"
;; To avoid this special treatment of $ and \, you must escape them with
;; \. Because it is in a Clojure string, to get one \ we must escape
;; *that* with its own \.
user=> (str/replace-first "fabulous fodder foo food" #"f(o+)(\S+)" "\\$2\\$1")
"fabulous $2$1 foo food"
;; To ensure the replacement is treated literally, call
;; java.util.regex.Matcher/quoteReplacement on it. A shorter name
;; like re-qr can be handy.
user=> (import '(java.util.regex Matcher))
java.util.regex.Matcher
user=> (defn re-qr [replacement]
(Matcher/quoteReplacement replacement))
#'user/re-qr
user=> (str/replace-first "fabulous fodder foo food" #"f(o+)(\S+)" (re-qr "$2$1"))
"fabulous $2$1 foo food"
(defn ^String replace-first
"Replaces the first instance of match with replacement in s.
match/replacement can be:
char / char
string / string
pattern / (string or function of match).
See also replace-all."
{:added "1.2"}
[^CharSequence s match replacement]
(let [s (.toString s)]
(cond
(instance? Character match)
(replace-first-char s match replacement)
(instance? CharSequence match)
(.replaceFirst s (Pattern/quote (.toString ^CharSequence match))
(.toString ^CharSequence replacement))
(instance? Pattern match)
(if (instance? CharSequence replacement)
(.replaceFirst (re-matcher ^Pattern match s)
(.toString ^CharSequence replacement))
(replace-first-by s match replacement))
:else (throw (IllegalArgumentException. (str "Invalid match arg: " match))))))
Comments top
No comments for replace-first. Log in to add a comment.