3

I have this code to replace all store's name is ABC (or Abc ...) to XYZ

(def str1 "ABC store of JOHN")
(str/replace (str/lower-case str1) #"abc" "XYZ")

// output: XYZ store of john
// expected: XYZ store of JOHN

I don't want to lowercase all the string like that. But in Clojure, it can't use the regex /abc/i with i flag for ignore case sensitive like other languages.

What kind of clojure regex or clojure lib support case sensitive?

2 Answers2

6

But in Clojure, it can't use the regex /abc/i

Yes, you can:

(let [str1 "ABC store of JOHN"]
    (str/replace str1 #"(?i)abc" "XYZ"))
akond
  • 15,865
  • 4
  • 35
  • 55
0

Clojure simply uses slightly different regex language that it inherited from Java, so you need to write regex as following:

(str/replace (str/lower-case str1) #"(?:abc)" "XYZ")

outputs "XYZ store of john"

You can find description of Java's regex language in the JDK documentation for Pattern class.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132