title: Regular expression for Custom Decimal
body: <p>My definition of <strong>Custom Decimal =></strong> The number can be represented as (p,s) where p is the precision and s is the scale and then describe everything with respect to precision and scale. Use Custom Decimal if you need to store decimals that have more than eight digits in the fractional part. Can have a maximum scale of 29 and a maximum precision of 30. The non-fractional part of the number can have a maximum of 29 digits. </p>
  
  <p>P and S may vary...</p>
  
  <p>Assume P= 18 S=12 i.e/ (P,S) = (18,12)</p>
  
  <p>intLength = 12</p>
  
  <p>fractionalLength=18</p>
  
  <p><strong>Piece of code</strong></p>
  
  <pre><code>String regExprCD = "^-?\\d{1,"+intLength+"}(?&gt;\\.\\d{0,"+fractionalLength+"})?$";
  
                      info("regExprCD = "+regExprCD );
                      if (value.matches(regExprCD ) || value.isEmpty()){
                          Window.alert("Valid CUSTOM_DECIMAL value");
                      }else{
                          Window.alert("Valid CUSTOM_DECIMAL value");
                      }
  </code></pre>
  
  <p>I tried following regular Expression but error comes on page <strong>"Syntax error in regular expression"</strong></p>
  
  <p><strong><code>^-?\\d{1,"+intLength+"}(?&gt;\\.\\d{0,"+fractionalLength+"})?$</code></strong> </p>
  
  <p>Which is perfect regular expression to allow following cases :</p>
  
  <p><strong>Regular expression should accept following Custom Decimal:</strong></p>
  
  <p>123456789012345678</p>
  
  <p>1234567.1234567890</p>
  
  <p>1.123456789012</p>
  
  <p>1234567890.1234567</p>
  
  <p>12345678901234567.</p>
  
  <p>12345.</p>
  
  <p><strong>Regular expression should not accept following Custom Decimal:</strong></p>
  
  <p>12345678901234567890 : Reason P > 18 :: P should not be greater than 18</p>
  
  <p>1234567.1234567890123 : Reason s>12 : :: S should not be greater than 12</p>
  
  <p>.1 or .1232 :: Invalid</p>
  
  <p>I used <strong><code>^-?\\d{1,"+intLength+"}(?&gt;\\.\\d{0,"+fractionalLength+"})?$</code></strong>  this regular expression.</p>
  
  <p>This regular expression working fine in Sample java program. Not working in GWT</p>
  
  <p>Throwing error on web page (developed in GWT) :: "Syntax error in regular expression"</p>
  
  <p>what should be data type of intLength  and fractionalLength ? int or string ? </p>
  
  <p><strong>Which is perfect regular expression ?</strong></p>
  
  <p>Any help or guidance in this matter would be appreciated. </p>