How do I create a regex that matches (p,s) precision and scale like the SQL decimal type?
<p>Precision (p): Total number of digits to the left and right of the decimal</p>

<p>Scale (s): Total number of digits to the right of the decimal</p>

<p>Consider my following regex so far:</p>

<pre><code>^-?[0-9]+(\.[0-9]{1,3})?$
</code></pre>

<ul>
<li>-? optional negative number</li>
<li>[0-9] matches numbers 0-9</li>
<li>"+" any number of digits</li>
<li>"(\.[0-9]{1,3})?" optional decimal with 1-3 digits</li>
</ul>

<p>Example:</p>

<pre><code>100.95 has a precision of 5 and a scale of 2 (5,2)
</code></pre>

<p>I know how to restrict total numbers to the left, and total numbers to the right, but not sure how to encapsulate the entire value to limit the "p, precision" part, and ignore the period if it exists in that count. The <strong>-</strong> also needs to be ignored in that total count.</p>

<p><strong>UPDATE</strong>:</p>

<p>This seems to be working... </p>

<pre><code>^(?=(\D*\d\D*){0,5}$)-?([0-9]+)?(\.?[0-9]{0,2})?$

blank line matches
0 - match
1 - match
123 - match
123.12 - match
-1 - match
123.122 - no match
</code></pre>