I have a String in Java that I want to check for whitespace and special characters excluding underscores and periods. What would be the best way to do so?
UPDATE
This is how I solved this particular problem:
String actualString = "string";
String testString = actualString;
testString = testString.replaceAll("_", "");
testString = testString.replaceAll("\\.", "");
Pattern whiteSpaceP = Pattern.compile("\\s", Pattern.CASE_INSENSITIVE);
Matcher whiteSpaceM = whiteSpaceP.matcher(testString);
Pattern specCharP = Pattern.compile("\\W", Pattern.CASE_INSENSITIVE);
Matcher specCharM = specCharP.matcher(testString);
if(whiteSpaceM.find() || specCharM.find()) {
System.out.println("There are spaces and non-alphanumeric characters excluding . and _ in the string");
}