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.
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:
((d+),)*(d+)
or
(d+)(,(d+))*
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):
(\d+,)*(\d+)
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.
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
'100,200,'
or
'100,200,,'
or
'100,,200'
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!
That’s it:
The anchors
^$will make the difference because they will force the whole string (not just a part) to match the pattern