regex in edn?

648 views
Skip to first unread message

Ikuru Kanuma

unread,
Mar 29, 2019, 3:59:48 AM3/29/19
to Clojure
Hi, just writing this out of pure curiosity.

Background:

1. I thought I could write regex literals like this in an edn file containing configurations:

:some-parameter #"foo"

But reading this with e.g. clojure.edn/read-string causes an error

No dispatch macro for: "

2. I thought I can introduce my own reader tag https://github.com/duct-framework/core/pull/21 but then thought it would be nice if there was a #regex "foo" reader tag defined in clojure itself.

3. Also noticed that this round trip does not work:

(clojure.edn/read-string (pr-str #"foo"))

4. The idea of a regex tag has been declined https://github.com/edn-format/edn/issues/26

I agree it is such really minor itch to scratch, but is the recommended approach to use a custom reader tag if one wishes to convey regex over wire?

Sorry for the attention and thanks!



Alexander Yakushev

unread,
Mar 29, 2019, 5:29:16 AM3/29/19
to Clojure
Not adding much to what you've already said, but a custom reader tag is exactly what I would reach out for in this situation.

Ikuru Kanuma

unread,
Mar 29, 2019, 11:14:44 AM3/29/19
to Clojure
Thanks for the reply! Glad to have confirmations.

Matching Socks

unread,
Mar 29, 2019, 9:07:26 PM3/29/19
to Clojure
How would you resolve the problem "Rich Hickey" mentioned on the page you linked?  "The lack of portability for regexes"...  True, some regex patterns mean the same thing in the JVM and JS, but in general a regex pattern is not a portable value; it may mean something different to each regex library.  Worse (or better), suppose you wrote a Java regex into the EDN file, and tried to read the EDN in JS, and the regex constructor threw an exception?

A slightly more esoteric issue might also arise.  https://github.com/edn-format/edn says,

"edn is a system for the conveyance of values. ... there are no reference types, nor should a consumer have an expectation that two equivalent elements in some body of edn will yield distinct object identities when read.... Thus the resulting values should be considered immutable, and a reader implementation should yield values that ensure this, to the extent possible." 

But JS' RegExp is highly mutable, as it blends the features of Java's Pattern and Matcher.

In the end, you might as well convey regex patterns as strings in EDN and confront the portability question in whatever way suits your purposes.  If regexes were added to EDN, either they would not be real platform regexes, or it would not really be EDN anymore!

Alex Miller

unread,
Mar 29, 2019, 9:53:04 PM3/29/19
to Clojure
There are edn impls in at least 15 languages so the problem is wider than just java and js.
Reply all
Reply to author
Forward
0 new messages