On Sat, Feb 21, 2009 at 1:34 PM, Frantisek Sodomka <fsodo...@gmail.com> wrote: > > nth claims to also work on regex Matchers: > > user=> (doc nth) > ------------------------- > clojure.core/nth > ([coll index] [coll index not-found]) > Returns the value at the index. get returns nil if index out of > bounds, nth throws an exception unless not-found is supplied. nth > also works for strings, Java arrays, regex Matchers and Lists, and, > in O(n) time, for sequences. > > Please, could somebody show me an example of that? I tried things > along the lines of: > > user=> (re-seq #"a" "ababaa") > ("a" "a" "a" "a") > user=> (re-matcher #"a" "ababaa") > #<Matcher java.util.regex.Matcher[pattern=a region=0,6 lastmatch=]> > user=> (nth (re-matcher #"a" "ababaa") 0) > java.lang.IllegalStateException: No match found (NO_SOURCE_FILE:0) > user=> (seq (re-matcher #"a" "ababaa")) > java.lang.IllegalArgumentException: Don't know how to create ISeq > from: Matcher (NO_SOURCE_FILE:0) > > ... but no success so far.
Valiant attempts, all. But Matcher is a mutable Java object -- you're trying to use nth on it before it's been fully initialized. Calling it's find method (or using the single-arg form of re-find) will cause it to refer to the first match in the string, at which point nth can be used to pick out the group you want: user=> (def m (re-matcher #"(a)(b)" "ababaa")) #'user/m user=> (re-find m) ["ab" "a" "b"] user=> (class m) java.util.regex.Matcher user=> (nth m 1) "a" I would recommend avoiding using re-matcher or the single-arg re-find unless you're sure you need the performance or flexibility in some particular kind of loop. It's much more pleasant to work with re-seq. --Chouser --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---