title: Why the following regular expression is not allowing numbers?
body: <p>Well, this might sound as if it is a duplicate question but it is not. I have asked this question in connection with <a href="https://stackoverflow.com/questions/29776908/filtering-a-jtextfield-for-strings-space-and-a-dot-using-a-documentfilter">this question here</a>. I have rewritten my <code>DocumentFilter</code> to use regular expressions. In validating a person's name I want only the following characters <code>[a-zA-Z]</code>,<code>'</code>,<code>\S</code> and <code>.</code>. </p>
  
  <p>I have written my regular expression with the hope that it will solve this. It is working the way I want it <strong>but the fact that it is not allowing numbers when I have not yet set it to do so is puzzling me.</strong> </p>
  
  <p><strong>Question:</strong> Why is <code>regex</code> not allowing numbers?</p>
  
  <p>This is the regular expression <code>[\\_\\(\\)@!\"#%&amp;*+,-:;&lt;&gt;=?\\[\\]\\^\\~\\{\\}\\|]</code> and what it should not allow to be entered is commented in the code below:</p>
  
  <p>My DocumentFilter is as follows:</p>
  
  <pre><code>public class NameValidator extends DocumentFilter{
  @Override
  public void insertString(FilterBypass fb, int off
                      , String str, AttributeSet attr) 
                              throws BadLocationException 
  {
      // remove 0-9 !"#$%&amp;()*+,-/:;&lt;=&gt;?@[\]^_`{|}~
      fb.insertString(off, str.replaceAll("^[\\_\\(\\)@!\"#%&amp;*+,-:;&lt;&gt;=?\\[\\]\\^\\~\\{\\}\\|]", ""), attr);
  } 
  @Override
  public void replace(FilterBypass fb, int off
          , int len, String str, AttributeSet attr) 
                          throws BadLocationException 
  {
      // remove 0-9 !"#$%&amp;()*+,-/:;&lt;=&gt;?@[\]^_`{|}~
      fb.replace(off, len, str.replaceAll("^[\\_\\(\\)@!\"#%&amp;*+,-:;&lt;&gt;=?\\[\\]\\^\\~\\{\\}\\|]", ""), attr);
     }
  }
  </code></pre>
  
  <p>Here is my test class:</p>
  
  <pre><code>public class NameTest {
  
  private JFrame frame;
  
  public NameTest() throws ParseException {
      frame = new JFrame();
      initGui();
  }
  
  private void initGui() throws ParseException {
  
      frame.setSize(100, 100);
      frame.setVisible(true);
      frame.setLayout(new GridLayout(2, 1, 5, 5));
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      JTextField name = new JTextField(10);
      ((AbstractDocument)name.getDocument()).setDocumentFilter(new NameValidator());
      frame.add(name);
  
  }
  
  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
  
          @Override
          public void run() {
              try {
                  NameTest nt = new NameTest();
              } catch (ParseException e) {
  
                  e.printStackTrace();
              }
  
          }
       });
      }
    }
  </code></pre>