title: Regex with comma separated numbers in Apex
body: <p>As a preface, I realize there are other topics on regular expressions with comma separated numbers, but when I tried to use those solutions, they didn't work.</p>
  
  <p>Basically, I am trying to create a regular expression to recognize comma separated numbers (in this case without spaces). Before trying to convert this into actual regex syntax, I realize that it should probably work something like this, where 'd' is a number and ',' is a comma, and '+' is a kleene plus:</p>
  
  <pre><code>((d+),)*(d+)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>(d+)(,(d+))*
  </code></pre>
  
  <p>Here's the code I am using in an Apex validation to make sure that a certain field is a list of numbers separated by commas without spaces (note: I have tried several variations of this to no avail, but will only post one):</p>
  
  <pre><code>(\d+,)*(\d+)
  </code></pre>
  
  <p>For some reason this isn't working, but it seems to be the correct syntax of any digit 1 or more times followed by a single comma, and that entire expression can be repeated 0 or more times, and that entire repeated expression should always be followed by at least 1 digit. </p>
  
  <p>This expression in practice does recognize all the accepted forms (ex: 100 or 100,200 etc.), but for some reason it also accepts answers like </p>
  
  <pre><code>'100,200,'
  </code></pre>
  
  <p>or </p>
  
  <pre><code>'100,200,,'
  </code></pre>
  
  <p>or </p>
  
  <pre><code>'100,,200'
  </code></pre>
  
  <p>I'm pretty stumped as to why this won't work as well as the previously given solutions which seem to do the same thing mine do. Thanks for any help in advance!</p>