I validate my input if it passes both regex above.
How can I tweak both regex so that it accepts a list ie (like on input2 and input3). Right now my regex only work on input1.
2 or higher:
^\d{2}\d*$
non 0:
^[1-9]\d*$
input1: 123
input2: 123, 456
input3: 123, 456, 789
First of all, the pattern you posted is equivalent to
^\d{2,}$, which requires the number to have two or more digits. The regex for an integer greater than or equal to 2 is more like^[+-]?0*([2-9]|[1-9]\d+)$. From your description, it’s not clear which of these you intended.Either way, what you want to use is something like this:
So for your scenario, it would be something like:
I’m not sure what flavor of regex you’re using, but if your engine balks at the redundant use of
$in the patterns above, you could try something likeinstead. Example:
Bear in mind that a better way to do this is usually to split the string on the comma + whitespace separator, which will give you an array of strings you can try to parse as integers.