Reputation: 41
In Java, is there a way to find out if string contains multiple letters/characters using Regular Expressions/ Pattern matching?
I have tried to solve my problem using below code
String id = "A12B45";
Pattern pattern = Pattern.compile("[A-Z]*");
Matcher matcher = pattern.matcher(id);
if (matcher.find())
{
System.out.println("YES---");
} else
{
System.out.println("NO---");
}
The above code is not giving me an output which I want. When string contains more than one alphabet letter then it should return "YES---". Can you please help?
Upvotes: 0
Views: 191
Reputation: 370679
Match an alphabetical character, then anything but alphabetical characters, then an alphabetical character again:
Pattern pattern = Pattern.compile("[A-Z][^A-Z]*[A-Z]");
Upvotes: 1