1. The Clojure wrapper put the last two calls inside the if, unlike
Matcher#replaceFirst(), thus the nil.

public String replaceFirst(String replacement) {
     if (replacement == null)
         throw new NullPointerException("replacement");
     StringBuffer sb = new StringBuffer();
     reset();
     if (find())
         appendReplacement(sb, replacement);
     appendTail(sb);
     return sb.toString();
}

(defn- replace-first-by
  [^CharSequence s ^Pattern re f]
  (let [m (re-matcher re s)]
    (let [buffer (StringBuffer. (.length s))]
      (if (.find m)
        (let [rep (f (re-groups m))]
          (.appendReplacement m buffer rep)
          (.appendTail m buffer)
          (str buffer))))))

On Mar 10, 4:39 pm, Takahiro Hozumi <fat...@googlemail.com> wrote:
> Hi,
> I have two questions about clojure.string/replace-first.
>
> 1.
> Is this expected behavior of replace-first?
>
> (require '[clojure.string :as str])
> (str/replace-first "abc def" #"ghi" (fn [a] (str a a)))
> => nil
>
> I don't think so, because string / string argument version returns
> original string when mismatched.
> (str/replace-first "abc def" "ghi" "jkl")
> => "abc def"
>
> 2.
> Is it difficult that replace-first support argument of string /
> function besides regex / function (i.e. without using Pattern/quote)?
>
> (str/replace-first "abc def" "def" (fn [a] (str a a)))
> => "abc defdef"
>
> The reason why I'd like to avoid using Pattern/quote is that ,I think,
> to replace string with string should be more lightweight than with
> regex.
>
> Thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to