title: Regular expression to match number with Decimal separator and optional Thousands separator
body: <p>I need to validate if a number is valid with using decimal separator and thousand separators optionally. I need a regex, because the current code, that is not done by me uses a regex that is not working properly and I am aiming to fix it.</p>
  <p>So the following combinations would be legal (Using <code>.</code> as decimal separator and <code>,</code> as thousand separator)</p>
  <ul>
  <li>10000</li>
  <li>100,000,000</li>
  <li>100,000,000.345345</li>
  <li>10000.689</li>
  </ul>
  <p>But the following wouldn't be legal:</p>
  <ul>
  <li>10000,000,000.34534</li>
  </ul>
  <p>Basically I want numbers with a proper use of decimal and thousand separator or without them at all, but not invalid combinations.
  I looked around a lot, so far I found <a href="https://stackoverflow.com/a/19641268/752842">this post</a>. Which has a great regex that matches perfectly when the numbers have decimal separator and thousand separator, but when I try to add the other options (numbers only with decimal separator, or number integers) I break the expression.</p>
  <p>I decided that maybe I could capture groups matching the rules with or, so the number matches one expression or the other like this:</p>
  <pre><code>^([+-]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?)|(((\d)*)+?(\.\d))|(\d*)$
  </code></pre>
  <p>The first group is the expression from the other post, the second I think covers numbers with a decimal separator and the last one are integers, but it is not working.</p>
  <p>For example these numbers are not getting matched:</p>
  <ul>
  <li>10000000.56</li>
  <li>10000000</li>
  </ul>
  <p>I think the issue it is happening because it always test for the first group, but I am not sure how to use the groups properly so it matches either of them. Also I think I am placing the -+ signs in the wrong place.</p>
  <p>I did search a lot, and couldn't find one expression that worked properly. From the ones suggested:</p>
  <p>From the <a href="https://stackoverflow.com/questions/16148034/regex-for-number-with-decimals-and-thousand-separator">Regex for number with decimals and thousand separator</a> post</p>
  <ul>
  <li><code>((\d){1,3})+([,][\d]{3})*([.](\d)*)?</code>  -&gt; It wrongly captures 10000000. and 10,000,000.</li>
  <li><code>^(?!0+\.00)(?=.{1,9}(\.|$))\d{1,3}(,\d{3})*(\.\d+)?$</code> -&gt; it fails for 10000000</li>
  <li><code>/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/</code> -&gt; it fails for 10000000</li>
  <li><code>((\d){1,3})+([,][\d]{3})*([.](\d)*)?</code>  -&gt; It fails for -100.03</li>
  </ul>
  <p>This <a href="https://stackoverflow.com/questions/3378773/can-i-use-an-or-in-regex-without-capturing-whats-enclosed">Can I use an OR in regex without capturing what's enclosed?</a> post is not a duplicate, it explains something that could help me solve the issue, but it is not a duplicate, especially because I have issues also with the +- signs. Also my questions covers a problem that is not particular to me, this is a problem many people face, yet from the implementations I have found so far, none work.</p>
  <p>From <a href="https://stackoverflow.com/questions/6248621/regex-matcing-numeric-values-with-or-without-thousand-separators">RegEx matching numeric values with or without thousand separators</a> post</p>
  <ul>
  <li><code>^-?([0-9]{1,3}\.?)+$</code> -&gt; It fails for -1000 but not for and not for 100.00.3</li>
  <li><code>^-?(?!0)(?:\d+|\d{1,3}(?:\.\d{3})+)$</code> -&gt; It fails for -100.03</li>
  </ul>