0

I need to delete some contents at the end of a string (eq_code) using Clojure. I want to implement a regex initialized by a variable (get-in vector [3 1]).

Maybe macro would have helped me.

Here is the code :

(reset! atom_code (clojure.string/replace eq_code #(str (get-in vector [3 1])) ""))

The error is :

IllegalArgumentException Invalid match arg: project.core$interpreted_lang_while$fn__4457@7ac4b7c5  clojure.string/replace (string.clj:102)

Is there a way to replace a substring without using macro ? For example using a function that return a regex.

1
  • Your code there passes an anon-function #(str ...) to str/replace. Commented Dec 30, 2018 at 9:28

2 Answers 2

3

https://clojuredocs.org/clojure.string/replace

replace match parameter (second arg) can't be a function. The solution is to construct the pattern from your dynamic value:

user> (def data [["a" "b"] ["c" "d"]])
#'user/data

user> (clojure.string/replace "mama" (re-pattern (get-in data [0 0])) "")
"mm"

and also: vector is the core function, so try not to shadow it by using it as a var name (though it's not actually what is wrong with your solution)

Sign up to request clarification or add additional context in comments.

1 Comment

That is what I was looking for. Thanks
0

I think the problem is that vector is a clojure function, and I am guessing that you have some data named vector that is causing the compiler confusion.

Could you please update your question with sample data and desired output?

1 Comment

Finally, I have found a workaround to my problem. I no longer need using (string/replace). Thanks you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.