我想验证基于Java的应用程序中的文本字段,我想只允许从0到9的逗号分隔数字

时间:2016-12-15 05:14:21

标签: java regex

我想验证基于Java的应用程序中的文本字段,我希望只允许从0到9的逗号分隔数字。 例如 {0,1,2,3,4,5,6,7,8,9} 正则表达式,以任何顺序接受最多10位数。

2 个答案:

答案 0 :(得分:1)

如果你是普通的正则表达式,你可以试试这个:

^\d[\d,]{0,18}[^,]$

修改

需求变更后。你可以试试这个正则表达式:

^(?:(([\d]),)(?!.*\2)){0,9}\d$

它会检查unqure数字

正则表达式解释:

^                          - if starts with 
(([\d]),)                  - look for a digit followed by comma and capture the digit 
(?!.*\2)                   - don't allow any other characters or previously matched digits (`\2` matches the previous group which is digits matched in previous step)
(?:(([\d]),)(?!.*\2))       - don't capture the group (capture the entire string)
{0,9}                       - allow only 0-9 occurrences of previous match (9 chars for numbers and 9 chars for commas)
\d$                        - should end with a digit (10th digit)

答案 1 :(得分:0)

使用此正则表达式:

\{\d(,\d){0,9}\}

演示:http://regexr.com/3esnc