自定义小数和千位分隔符的正则表达式

I am using the below regex the handle the custom thousand separator which could be any of the , or . or space character which works for the thousand separator and not for the decimal indicator.

I am trying to add a new capturing group to handle decimal indicator (, or .) with maximum 2 decimals but the regex breaks for thousand separator with it.

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

如何添加捕获组以使用自定义字符处理小数?有任何想法吗?

有效输入:

1234
123.45
123,45
1234.56
1234,56
123
1,234
12,345
1,234,567
12,345,678
123,456,789

12
1.234
12.345
1.234.567
12.345.678
123.456.789

123
1 234
12 345
123 456
1 234 567
12 345 678
123 456 789

123.4567
123,4567
123 4567

1,345.67
1.345,67
1 345.67

12,345.67
12.345,67
12 345.67
123,456,789.34
123.456.789,34
123 456 789.34

无效:

12.345.67
12,345,67
12 345 67
123 456 789 34
评论
  • liste
    liste 回复

    以下正则表达式将匹配示例中的所有情况:

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

    最后一部分(?:[,。]?\ d +?)*使小数部分的匹配成为可选。