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");
}
Just exclude using ^
the alphabetical letters:
[^a-zA-Z]+
See demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With