https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Christian Sousa

07/28/2020, 10:46 AM
Anyone knows why this is giving me a return value of false when it should be true?
"\b[01]+\b".toRegex().matches("11010011")
Basically what I’m trying to do is just check if there is something else besides 0 or 1 in the string. I tried also with
"/^[0-1]+$/".toRegex().matches("11010011")
But with the same result..
o

okarm

07/28/2020, 10:51 AM
Remove the slash characters from your second pattern
s

spand

07/28/2020, 10:51 AM
Dont use
/
c

Christian Sousa

07/28/2020, 10:51 AM
that was it.. thanks!
One question, why is it like this?
s

spand

07/28/2020, 10:53 AM
Modifiers are not given in the regex but as parameters to
toRegex()
so there is no need to use the
/
delimiters
o

okarm

07/28/2020, 10:57 AM
Also any JetBrains IDE should warn you about this.
👍 1
c

Christian Sousa

07/28/2020, 10:58 AM
Oh, didn’t noticed, and I was just trying stuff out in the kotlin playground instead of the IDE 😛
Thanks for your help!
m

Matteo Mirk

08/12/2020, 9:57 AM
If you hit
alt+Enter
on the string you get the “check” intention to test your expression in IntelliJ.
2 Views