https://kotlinlang.org logo
Title
j

jermainedilao

07/25/2018, 7:09 AM
Hi! can someone enlighten me why this returns false?
"hello world".matches(Regex("([A-Za-z])"))
l

Lucas Ł

07/25/2018, 7:10 AM
because your regex is expecting a single letter from A-z Try using this website (or any similar) if you are new to regular expressions https://regexr.com/
r

robin

07/25/2018, 7:13 AM
Yeah,
matches
tries to match the entire input, even if that's not really stated in the Kdoc. This returns true:
"hello world".matches(Regex("(.*[A-Za-z].*)"))
l

Lucas Ł

07/25/2018, 7:15 AM
Returns true if this char sequence matches the given regular expression.
I see how the choice of words might be confusing to some... For the match you can use even simpler
"[A-z\s]*"
j

jermainedilao

07/25/2018, 7:24 AM
Right! 💡thanks guys 👍
g

gildor

07/25/2018, 7:28 AM
@jermainedilao Please, do not cross-post your messages to different channels. Choose one. And this #android channel is a bad choice for such generic questions
j

jermainedilao

07/25/2018, 7:29 AM
@gildor my bad. Noted!
a

Allan Wang

07/25/2018, 6:53 PM
To add on, I believe there’s a method
contains
to just check if the regex matches any segment for the string. And if you are checking for letters only, you can do
Regex([A-Za-z]*)
(which wouldn’t match hello world due to the space)