Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this regex doesn't validate special characters?

Why does the following regex expression in kotlin does not validate special characters? What constitutes as special character?

private fun validateSpecialCharacter(password: String): Boolean =
        password.matches(Regex("[!@#\$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]"))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Using CharSequence.matches(regex) returns True if the entire string matches the regex. Given your code, it looks as if you want your function to return True as long as there is an occurrence matching the regex.

Using CharSequence.contains(regex) instead should yield your desired results. Your code becomes:

private fun validateSpecialCharacter(password: String): Boolean =
        password.contains(Regex("[!@#\$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading