Regular expressions do not support ^start and end$ of input characters?

66 views
Skip to first unread message

Dominykas Mostauskis

unread,
Apr 19, 2015, 11:39:28 AM4/19/15
to clojur...@googlegroups.com
I am having trouble using the ^ and $ characters under Clojurescript. Equivalent calls in Clojure work fine. Workarounds?

Unexpected cljs behaviour:

cljs.user=> (clojure.string/split "123" #"$" 2)
["" "123"]
cljs.user=> (clojure.string/split "123" #"^" 2)
["" "123"]

Clojure works fine:

user=> (clojure.string/split "123" #"$" 2)
["123" ""]
user=> (clojure.string/split "123" #"^" 2)
["123"]

Francis Avila

unread,
Apr 20, 2015, 3:12:29 PM4/20/15
to clojur...@googlegroups.com
This is an edge case of clojure.string/split with limits and non-consuming assertions. ^ and $ are fully supported with the meaning of the underlying regex engine (which may be subtly different in javascript and java).

This is clearly wrong. It occurs because clojure.string/match uses the size of the match to determine where to split. Since the match is always empty, it always splits at index 0.

cljs.user=> (clojure.string/split "123" #"$" 2)
["" "123"]


However, I'm not sure why you consider Clojure's answers to be correct. I would expect this in Clojure:

user=>(clojure.string/split "123" #"^" 2)
["" "123"]



Dominykas Mostauskis

unread,
Apr 20, 2015, 4:32:28 PM4/20/15
to clojur...@googlegroups.com
I had the afterthought that my examples were a bit odd. As far as splitting with regex is concerned Clojure and Clojurescript behaviour is significantly different in my experience. In my use case I gave up trying to find regex patterns that would work for both clj and cljs and started writing platform specific patterns. Luckily Clojure 1.7.0 is being rolled out at the moment and it includes Reader Conditionals allowing to write these bilingual libraries easily.

--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/v8TqycN6ZMQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Reply all
Reply to author
Forward
0 new messages