Hi! can someone enlighten me why this returns fals...
# android
j
Hi! can someone enlighten me why this returns false?
Copy code
"hello world".matches(Regex("([A-Za-z])"))
l
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
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
Copy code
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
Right! 💡thanks guys 👍
g
@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
@gildor my bad. Noted!
a
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)