How to check a string in Java equals to a regex pattern?

java, regex

Solution

Use `String#matches()` and a regex:

if (str.matches("[\\d\\s]+"))
    // string is acceptable

Problem

Lets say I have a string and it could be 1234 or 12 34 or 1 2 3 4 5 It doesn't matter about the number of digits or whitespace, just so that it accepts a string that has only digits and if there is whitespace within string of digits it will still accept it. How would I write the regex pattern for this?

Original source