napi15 napi15 - 2 years ago 73
Java Question

Regex pattern for at least 1 char

I'm trying to setting a regex pattern that checks if a text has at least 1 character could be upper case or lower it won't matter

Text exemple : Hello world <---good pattern

I tried both of these and it always return false

boolean b = Pattern.matches("/[A-Za-z]", title); //it return false :( why?

I also tried

boolean b = Pattern.matches("/^[A-Za-z]+$/", title ); //it return false :( why?

I need some help understanding what I'm doing wrong

Much appreciation


Answer Source

Your "good" String also includes white space (and your regular expression shouldn't include /s). Something like,

String title = "Hello world";
boolean b = Pattern.matches("^[A-Za-z\\s]+$", title);

Will set b to true.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download