How to compare two regular expressions in Clojure?

Im unit testing a function that creates a regular expression but using =does not work. How can I verify that it returns the correct regular expression?

Here is what I tried for an empty regex:

(= #"" #"") ; false
(== #"" #"") ; ClassCastException java.util.regex.Pattern cannot be cast to java.lang.Number
(identical? #"" #"") ; false
(.equals #"" #"") ; false

Is there a way for Clojure -ish to do this, or do I need to convert both regular expressions to strings and then compare them?

+4
source share
2 answers

unfortunately there is no better way, you just need to use strings

user> (= (str #"foo") (str #"foo"))    
true                                   
user> (= (str #"foo") (str #"fooo"))   
false 

Even this is not ideal, because it does not capture regular expressions that match the same lines, although they look different.

user> (re-seq #"[a]" "aaaa")       
("a" "a" "a" "a")                  
user> (re-seq #"a" "aaaa")         
("a" "a" "a" "a")
user> (= (str #"a") (str #"[a]"))  
false 

, . , Clojure == , , ( ).

+5

, clojure java.util.regex.Pattern. java- , , false.

- .

0

Source: https://habr.com/ru/post/1527795/


All Articles