Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string has no alphabets

Tags:

java

regex

I'm trying to find whether the string has only numbers and special characters.

I tried below code but it isn't working

String text="123$%$443";
String regex = "[0-9]+";
String splChrs = "-/@#$%^&_+=()" ;
if ((text.matches(regex)) && (text.matches("[" + splChrs + "]+"))) {
  System.out.println("no alphabets");
}
like image 336
Harini Avatar asked Nov 30 '22 08:11

Harini


1 Answers

Just exclude using ^ the alphabetical letters:

[^a-zA-Z]+

See demo

like image 182
user7294900 Avatar answered Dec 14 '22 21:12

user7294900