Re: Clojure regexs
lol oh noes! i should really stop doing this while watching 30rock... On Thu, Jan 13, 2011 at 9:38 PM, Ken Wesson wrote: > On Thu, Jan 13, 2011 at 10:22 PM, gaz jones wrote: >> bah! good catch. >> (let [[_ year month day]] (re-find date-regex line)) >> >> fixed! > > Oh, gaz. I'm so so sorry. :( > > -- > 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 -- 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
Re: Clojure regexs
On Thu, Jan 13, 2011 at 10:22 PM, gaz jones wrote: > bah! good catch. > (let [[_ year month day]] (re-find date-regex line)) > > fixed! Oh, gaz. I'm so so sorry. :( -- 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
Re: Clojure regexs
bah! good catch. (let [[_ year month day]] (re-find date-regex line)) fixed! On Thu, Jan 13, 2011 at 9:03 PM, Ken Wesson wrote: > On Thu, Jan 13, 2011 at 9:54 PM, Alex Baranosky > wrote: >> I see. So I may have to use some kind of clunky syntax instead of a nice >> $1, $2, $3 syntax. I can handle that I guess :) > > Hey, it's a Lisp! You can use almost any syntax you want. How about %1, %2, > %3: > > (defn re-do [f regex input] > (apply f (rest (re-find regex input > > (re-do #(do-stuff-with %3 %1 %2) regex input) > > Or even > > (defmacro re-let [names regex input & body] > `(let [[_ ~@names] (re-find ~regex ~input)] > ~@body)) > > (re-let [year month day] date-regex line > (do-something-with year month day)) > > This one doesn't even have the bug that gaz jones's let has. ;) > > -- > 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 -- 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
Re: Clojure regexs
On Thu, Jan 13, 2011 at 9:54 PM, Alex Baranosky wrote: > I see. So I may have to use some kind of clunky syntax instead of a nice > $1, $2, $3 syntax. I can handle that I guess :) Hey, it's a Lisp! You can use almost any syntax you want. How about %1, %2, %3: (defn re-do [f regex input] (apply f (rest (re-find regex input (re-do #(do-stuff-with %3 %1 %2) regex input) Or even (defmacro re-let [names regex input & body] `(let [[_ ~@names] (re-find ~regex ~input)] ~@body)) (re-let [year month day] date-regex line (do-something-with year month day)) This one doesn't even have the bug that gaz jones's let has. ;) -- 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
Re: Clojure regexs
For accessing groups in a match, you can use (re-matches). It will always give the full match as the first element though: user=> (re-matches date-regex "2011 1 13") ["2011 1 13" "2011" "1" "13"] So to replicate the Ruby code's behavior maybe you'll just want (rest (re-matches date-regex line)). For ignoring case, putting (?i) at the beginning of the regex worked for me. (re-find) should give you $1 just fine. user=> (require '[clojure.string :as str]) nil user=> (str/lower-case (re-find #"(?i)mon|tue|wed|thu|fri|sat|sun" "asdf MON tue")) "mon" Hope this helps! On Jan 13, 7:35 pm, Alex Baranosky wrote: > So I am converting some Ruby code I have into CLojure for practice/fun and I > am having trouble finding info via Google. > > I want to take something like this from Ruby and do it in Clojure: > > DATE_REGEX = /^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2})/ > > token =~ DATE_REGEX > [$1, $2, $3] > > So far my best guess has been: > > (defonce date-regex #"^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2})") > (re-find date-regex line) > > but I'm not sure how to access $1, $2, and $3 > > Another example I'd like to convert is this: > > DAYS_OF_WEEK_REGEX = /^\s*(#{Date::DAYS_OF_WEEK.join('|')})/i > > token =~ DAYS_OF_WEEK_REGEX > $1.downcase.to_sym > > Best attempt is: > > (defonce days-of-week-regex #"mon|tue|wed|thu|fri|sat|sun(?i)") > (re-find days-of-week-regex line) > > Here I'd love to know the syntax for ignore-case, along with how to access $1. > > Thanks, > Alex -- 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
Re: Clojure regexs
A blog entry I read (and now can't find) mentioned being able to use syntax like (?i) to do the ignore case. -- 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
Re: Clojure regexs
I see. So I may have to use some kind of clunky syntax instead of a nice $1, $2, $3 syntax. I can handle that I guess :) 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
Re: Clojure regexs
i think you want to do something like this: (let [_ year month day] (re-find date-regex line)) On Thu, Jan 13, 2011 at 8:50 PM, Eric Lavigne wrote: >> So I am converting some Ruby code I have into CLojure for practice/fun and I >> am having trouble finding info via Google. > > Clojure uses the same regex style as Java, so you'll need to search > for information on Java regexes rather than Clojure regexes. > > -- > 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 -- 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
Re: Clojure regexs
Here is one way: user=> (import 'java.util.regex.Pattern) java.util.regex.Pattern user=> (def p (Pattern/compile "mon|tue|wed|thu|fri|sat|sun" Pattern/CASE_INSENSITIVE)) #'user/p user=> (re-find p "I have a dentist appoint on Monday morning, so you'll need to take the kids to school.") "Mon" Bill On Thursday, January 13, 2011 8:35:17 PM UTC-6, Alex Baranosky wrote: > > > Here I'd love to know the syntax for ignore-case > > -- 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
Re: Clojure regexs
> So I am converting some Ruby code I have into CLojure for practice/fun and I > am having trouble finding info via Google. Clojure uses the same regex style as Java, so you'll need to search for information on Java regexes rather than Clojure regexes. -- 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
Clojure regexs
So I am converting some Ruby code I have into CLojure for practice/fun and I am having trouble finding info via Google. I want to take something like this from Ruby and do it in Clojure: DATE_REGEX = /^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2})/ token =~ DATE_REGEX [$1, $2, $3] So far my best guess has been: (defonce date-regex #"^\s*(\d{4})\s+(\d{1,2})\s+(\d{1,2})") (re-find date-regex line) but I'm not sure how to access $1, $2, and $3 Another example I'd like to convert is this: DAYS_OF_WEEK_REGEX = /^\s*(#{Date::DAYS_OF_WEEK.join('|')})/i token =~ DAYS_OF_WEEK_REGEX $1.downcase.to_sym Best attempt is: (defonce days-of-week-regex #"mon|tue|wed|thu|fri|sat|sun(?i)") (re-find days-of-week-regex line) Here I'd love to know the syntax for ignore-case, along with how to access $1. Thanks, Alex -- 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