title: How to check for whitespace and special characters are in string excluding underscores and periods
body: <p>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? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This is how I solved this particular problem:</p>
  
  <pre><code>            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");
              }
  </code></pre>