Regex for Comma Separated Number

后端 未结 1 1664
Happy的楠姐
Happy的楠姐 2020-12-18 08:14

I\'m trying to validate user input, which is just comma separated numbers. I\'d like to do this with RegEx, but can\'t come up with the right expression.

It should v

1条回答
  •  庸人自扰
    2020-12-18 08:48

    How about this:

    ^\d{1,3}([,]\d{3})*$
    

    Basically you can have 1-3 digits comma free. After that, you need a comma. If you've got a comma, it must be followed by 3 more digits. That comma-3-digit sequence can appear any number of times.

    EDIT: As Andrew Hare observed, you don't care about what was found inside the parentheses beyond the fact that it matched, so you can use a non-capturing group instead by placing ?: after the opening parenthesis:

    ^\d{1,3}(?:[,]\d{3})*$
    

    0 讨论(0)
提交回复
热议问题