Anyone good with regex? What is this actually spli...
# random
c
Anyone good with regex? What is this actually splitting my string on?
Copy code
myCowboy.split("\\$")
s
If it was a regex pattern, it would match a dollar sign
"\\" is escaping the
\
in the Kotlin string
so the actual regex pattern is
\$
e
in Kotlin, String.split(String) doesn't split on a pattern, it splits on a literal match
s
Yeah, I was just about to remember that
e
this is different (but saner) than Java's String.split
c
sorry. it is actually
Copy code
.split(regex = "\\$".toRegex())
i tried to simplify and bit myself
okay. so its splitting on a dollar sign. interesting. thank you
e
you could also write
Copy code
"""\$""".toRegex()
backslashes don't need to be escaped in multiline quotes
3
s
i recommend using online regex editors btw. they're a tremendous help and visualize what's happening in the matcher system
c
Yeah. I tried but what getting different results depending if I had an extra \ and so I was just confused if the online tool was getting escaped or not
k
However, In this particular case, you can just do
myCowboy.split('$')
❤️ 1
n
or
myCowboy.split("$")
. Normally, you would have to quote the
$
in strings if you want a literal
$
because of Kotlin's templating. However, a
$
at the end of a String literal does not require quoting.
1
e
$
before anything that doesn't look like interpolation will work without escaping
n
yup
e
"$0"
,
"$$"
,
"$[]"
, etc.
should you use that, maybe not. but it works