title: How to check if a string contains only digits in Java
body: <p>In Java for String class there is a method called matches, how to use this method to check if my string is having only digits using regular expression. I tried with below examples, but both of them returned me false as result.</p>
  
  <pre><code>String regex = "[0-9]";
  String data = "23343453";
  System.out.println(data.matches(regex));
  </code></pre>
  
  <hr>
  
  <pre><code>String regex = "^[0-9]";
  String data = "23343453";
  System.out.println(data.matches(regex));
  </code></pre>