This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-28
Channels
- # announcements (1)
- # aws (1)
- # babashka (41)
- # beginners (21)
- # biff (7)
- # calva (102)
- # cider (8)
- # cljs-dev (1)
- # clojure (8)
- # clojure-bay-area (2)
- # clojure-dev (30)
- # clojure-europe (40)
- # clojure-norway (52)
- # clojure-sweden (9)
- # clojure-uk (5)
- # clojurescript (15)
- # cursive (7)
- # data-science (1)
- # datomic (23)
- # events (1)
- # fulcro (9)
- # humbleui (23)
- # hyperfiddle (46)
- # introduce-yourself (1)
- # jackdaw (2)
- # jobs (2)
- # london-clojurians (1)
- # malli (13)
- # off-topic (8)
- # re-frame (36)
- # remote-jobs (1)
- # shadow-cljs (4)
- # specter (4)
- # squint (1)
- # transit (4)
- # vim (1)
user=> (str/replace "foo \\$bar baz" #"\\\$" "X")
"foo Xbar baz"
user=> (str/replace "foo \\$bar baz" #"\\\$" "$")
Execution error (IllegalArgumentException) at java.util.regex.Matcher/appendExpandedReplacement (Matcher.java:1075).
Illegal group reference: group index is missing
user=> (str/replace "foo \\$bar baz" #"\\\$" "\$")
Syntax error reading source at (REPL:1:42).
Unsupported escape character: \$
user=> (str/replace "foo \\$bar baz" #"\\\$" "\u0024")
Execution error (IllegalArgumentException) at java.util.regex.Matcher/appendExpandedReplacement (Matcher.java:1075).
Illegal group reference: group index is missing
user=> (str/replace "foo \\$bar baz" #"\\\$" (str \$))
Execution error (IllegalArgumentException) at java.util.regex.Matcher/appendExpandedReplacement (Matcher.java:1075).
Illegal group reference: group index is missing
user=>
How can I do this?answer:
user=> (str/replace "foo \\$bar baz" #"\\(\$)" "$1")
"foo $bar baz"
for this case. but how would I replace say all #"\+.*?\+"
patterns with "$"
?
(str/replace "foo \\$bar baz" #"\\\$" "\\$")
;; => "foo $bar baz"
seems to work for meThe Java platform's peculiarities might be shining through. Javadoc (https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-) says "Note that backslashes (`\`) and dollar signs (`$`) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll
. Use Matcher.quoteReplacement(java.lang.String)
to suppress the special meaning of these characters, if desired."
Definitely felt like something at the java level. Thanks all!
n.b. there's a clojure fn for this too: https://clojuredocs.org/clojure.string/re-quote-replacement
(str/replace "foo \\$bar baz" #"\\\$" (str/re-quote-replacement "$"
))
;; => "foo $bar baz"
I use str/re-quote-replacement
for more complex regex combinating but wouldn't have that to use it here. thanks.