[英]Java: Regular expression for the input field only numbers and commas + space characters
There is a field that is filled with numbers or groups of numbers. 有一个字段填充数字或数字组。
After the comma must be a space! 逗号后必须是空格!
The symbol " ,
" and the space cannot be the beginning and /
or at the end of the field 符号“
,
”和空格不能是字段的开头和/
或结尾
^[\d+]{1,}([,]{1}[\s]{1}).*[\d+]$
- this does not work. please help to write a regular expression according to the described condition.
example 例
1 - ok!
2, 3 - ok!
6, 7, 4 -ok!
,5 - bad!
5 6 0 - bad!
4,5 - bad!
You could use a repeating group with a space (or \\s
) prepended. 您可以使用前置空格(或
\\s
)的重复组。
In your pattern you could remove the .*
and match the last \\d+
inside the group. 在你的模式中你可以删除
.*
并匹配组内的最后一个\\d+
。 Then repeat the group 0+ times. 然后重复0次以上的组。
It would look like ^[\\d]{1,}([,]{1}[\\s]{1}[\\d]+)*$
它看起来像
^[\\d]{1,}([,]{1}[\\s]{1}[\\d]+)*$
Note that you don't have to put the \\d+
between square brackets or else the +
would be matched literally and the quantifier {1}
could be omitted: 请注意,您不必将
\\d+
放在方括号之间,否则+
将按字面匹配,并且可省略量词{1}
:
^\d+(?:, \d+)*$
In Java 在Java中
String regex = "^\\d+(?:, \\d+)*$";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.