I have this regular expression:
[^0-9!a-zA-z#\\$%&'\\*\\+\\-/=\\?\\^_`\\{\\|\\}~@\\.]+
and I am trying to split the email address using
[Email]info@emerycommunications.com
But the following code in java:
String fileStr = "[Email]info@emerycommunications.com";
String invalidCharacters = "[^0-9!a-zA-z#\\$%&'\\*\\+\\-/=\\?\\^_`\\{\\|\\}~@\\.]+";
String[] tokens = fileStr.split(invalidCharacters);
for (String token:tokens) {
if (token.contains("@")) {
System.out.println(token);
}
}
is giving this output:
[Email]info@emerycommunications.com
I am completely clueless as invalidCharacters variable covers [ and ] also.
You have
A-zin your character class, and the square bracket characters come between upper case Z and lower case a in ASCII (and Unicode) order. Thus]is being considered a valid rather than invalid character – presumably you meantA-Zinstead.