On Wed, Dec 3, 2008 at 2:57 PM, Blaine <[EMAIL PROTECTED]> wrote:
>
> I want to build a regular expression pattern literal (I think that's
> what they're called) like #"a.*" out of separate strings like "a" and
> ".*".
>
> user> (re-seq #"a.*" "bab")   ; this is cool
> ("ab")
> user> (re-seq #(str "a" ".*") "bab")  ; but this, in all its noobness, is not
> ; Evaluation aborted.
> user> #(str "a" ".*")
> #<user$eval__4569$fn__4571 [EMAIL PROTECTED]>  ; what
> is this thing?  can I use it somehow?

It's a function!  You could have built the same thing like this:
user=> (fn [] (str "a" ".*"))
#<user$eval__42$fn__44 [EMAIL PROTECTED]>

You can use it, but it has nothing to do with regex:
user=> (def thing #(str "a" ".*"))
#'user/thing
user=> (thing)
"a.*"

It's just a function that returns a literal string.  A "regex" in
Clojure/Java is actually a java.util.regex.Pattern:
user=> (class #"")
java.util.regex.Pattern

To programmatically build one, you'll have to use that class directly,
or 're-pattern':
user=> (doc re-pattern)
-------------------------
clojure.core/re-pattern
([s])
  Returns an instance of java.util.regex.Pattern, for use, e.g. in
  re-matcher.
nil
user=> (re-pattern (str "a" ".*"))
#"a.*"

...so back to your original example:
user=> (re-seq (re-pattern (str "a" ".*")) "bab")
("ab")

But remember that you may need extra backslashes inside the "string"
parts, compared to the equivalent #"regex" literal.

Or if each piece of your regex can stand alone, you could take
advantage of the fact that the .toString method on Pattern returns a
sufficiently escaped string:

user=> (re-seq (re-pattern (str #"s" #"\w*")) "sue sang a song")
("sue" "sang" "song")

--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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to