On Thu, Mar 28, 2013 at 5:49 PM, Mark Engelberg <mark.engelb...@gmail.com>wrote:

> I'm on 1.5.1 and I get that too, but even though:
> (pr #"a\nb") prints in a sane, readable way
> (pr (re-pattern "a\nb")) does not.
>
> The latter is what I need to print in a nice way.
>

Sorry, I missed that fine point.

When you say a "sane, readable way", do you mean human-readable, or
readable via clojure.core/read or clojure.core/read-string?

If the latter, then it already does print that way:

user=> (def p1 (re-pattern "a\nb"))
#'user/p1
user=> (def p2 (read-string (pr-str p1)))
#'user/p2
user=> p1
#"a
b"
user=> p2
#"a
b"

If you want clojure.core/read'ability but no actual newline character, I
don't think there is any way to create a syntax with #"" and only
non-newline characters between the quotes, such that the resulting regex
will contain the three characters a,newline,b as the original did.

If you don't need clojure.core/read'ability, then you can make up whatever
format you like, e.g. print the regex as a string with some type tag to
identify it as a regex.  This might even be readable by making your own
data-reader for it:

(defn print-regex-my-way [re]
  (print "#regex \"" (str re) "\""))

Note: clojure.core/read and read-string are not safe to read from anything
but trusted data sources, so I wouldn't recommend it for anything except
files you write yourself, or code.  See here for details if you are curious:

    http://clojuredocs.org/clojure_core/clojure.core/read

Also note that the new clojure.edn/read and read-string, and the edn
readers in the tools.reader contrib library, don't read Java regexes.  This
is by choice, I believe, with the reason given that regexes are not
portable across Java, JavaScript, etc. platforms.

    https://github.com/edn-format/edn/issues/26

Andy

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to