 <p>This is will match a string containing only 0 and 1's.</p> <pre><code>theString.matches("^[01]+$") </code></pre> <ol> <li>The character class [01] matches 0's or 1's</li> <li>The + says match 1 or more of the proceeding character class (so 0 or more 0's or 1's)</li> <li>The ^ and $ says that the entire string must match</li> </ol>