正则表达式:逗号分隔的整数 [英] Regex: Comma delimited integers
问题描述
我试图创建一个正则表达式接受:
一个空字符串,一个整数或多个整数,用逗号分隔,但不能有开始和结束逗号。
我设法找到了这个,但我不能undertsand如何删除数字限制
^ \d {1,10}([,] \d {10})* $
您发布的内容至少需要一个整数,因此不会与空字符串匹配:
这里是您需要的: / p>
^(\d +(,\d +)*)?$
/ pre>
说明:
- 将整个内容放在括号中
- 始终以整数开头,因此
'?'
'\d +'。
这是1个或多个数字字符('0' - '9' )
- 然后制作一组括号,其中包含
',\d +'
,然后在其后面加上星号。
3a。内部表示以,开始,然后是整数。
3b。星号表示重复括号内的所有内容0次或更多次。
Hench整个事件是一个空字符串或以整数开头,然后重复零或多次a以逗号开头并以整数结尾的字符
I'm trying to create a regex that accept: An empty string, a single integer or multiple integers separated by a comma but can have no starting and ending comma.
I managed to find this, but I cannot undertsand how to remove the digit limit
^\d{1,10}([,]\d{10})*$
解决方案The thing you posted still requires at least 1 integer, so it won't match an empty string:
Here is what you need:
^(\d+(,\d+)*)?$
Explaination:
- put the entire thing in parenthesis and end with a
'?'
so as to match the empty string.- Always start with an integer, so
'\d+'.
That is 1 or more digit characters('0'-'9')
- Then make a set of parenthesis which contains
',\d+'
and put an asterisk after it.
3a. The inside means start with a ',' then an integer. 3b. The asterisk means repeat everything inside the parenthesis 0 or more times.Hench the whole thing is
either an empty string or start with an integer then repeat zero or more times a string which starts with a comma and ends with an integer
这篇关于正则表达式:逗号分隔的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!