 <p>If you are using c#, you can simplify your life and use two separate regex:</p> <pre><code>bool res = false; string str = // your string if (str &lt; 8) { res = Regex.IsMatch(str, @"^[0-9]+$"); } else { res = Regex.IsMatch(str, @"^(?=[a-zA-Z0-9]*[a-zA-Z])[a-zA-Z0-9]*$"); } </code></pre>