This seems like a well known title, but I am really facing a problem in this.
Here is what I have and what I’ve done so far.
I have validate input string, these chars are not allowed :
&%$##@!~
So I coded it like this:
String REGEX = "^[&%$##@!~]";
String username= "jhgjhgjh.#";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(username);
if (matcher.matches()) {
System.out.println("matched");
}
Change your first line of code like this
And it should work fine.
^outside the character class denotes start of line.^inside a character class[]means a negation of the characters inside the character class. And, if you don’t want to match empty usernames, then use this regex