On Thu, Jun 25, 2009 at 4:05 PM, Chouser <chou...@gmail.com> wrote: > > On Wed, Jun 24, 2009 at 9:04 PM, Chouser<chou...@gmail.com> wrote: > > On Wed, Jun 24, 2009 at 6:13 PM, CuppoJava<patrickli_2...@hotmail.com> > wrote: > >> > >> I need to dynamically create a regex, and would really like to be able > >> to use Clojure's built-in regex syntax. But I don't know how to go > >> about it. > >> > >> My regex is: #"(.*?)(\(image .*)" > >> > >> except instead of "image", I need to dynamically insert an array of > >> possible strings to match. > >> > >> ie. given ["image" "buffer" "spacer"] > >> > >> I need to produce #"(.*?)(\((?:image|buffer|spacer) .*)" > > > > Here the best I've come up with for your example: > > > > (use '[clojure.contrib.str-utils :only [str-join]]) > > > > (defn my-regex [words] > > (re-pattern (str #"(.*?)" ; stands alone, so #"" works > > "(\\((?:" ; regular string, so \ must be escaped > > (str-join "|" words) > > ").*)"))) > > Had another thought: > > (defn my-regex [words] > (re-pattern > (.replace (str #"(.*?)(\((?:WORDS).*)") > "WORDS" > (str-join "|" words)))) > > user=> (my-regex ["image" "buffer" "spacer"]) > #"(.*?)(\((?:image|buffer|spacer).*)" > > That lets you use express more of the regex inside a #"", > though whether it's easier to understand in the end I'll > leave to you to decide. >
applying Pattern/quote on each word can be interesting (unless you expecte words to be a coll of pattern-strings). Christophe -- Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.blogspot.com/ (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 -~----------~----~----~----~------~----~------~--~---