正则表达式以逗号分隔的列表中的值范围

时间:2021-09-17 00:16:21

I want to write a regex that match a list of numeric values, given in a comma-separated list, ranges allowed. Empty is not allowed.

我想编写一个匹配数字值列表的正则表达式,以逗号分隔列表给出,允许范围。不允许空。

Something like: 1-10,20-56,8,7

类似的东西:1-10,20-56,8,7

So far I have (([0-9]+)|([0-9]+-[0-9]+),)*[0-9]+. This does most of the job, except it misses the case of one range only (eg: 1-10 would not validate).

到目前为止,我有(([0-9] +)|([0-9] + - [0-9] +),)* [0-9] +。这完成了大部分工作,除了它仅错过一个范围的情况(例如:1-10不会验证)。

The checking for each range can be omitted (eg: 20-10 can be allowed as a valid range).

可以省略对每个范围的检查(例如:允许20-10作为有效范围)。

Any help would be appreciated.

任何帮助,将不胜感激。

4 个解决方案

#1


6  

You can use the regex;

你可以使用正则表达式;

^([0-9]+(-[0-9]+)?)(,([0-9]+(-[0-9]+)?))*$

Regex in action

正则表达式在行动

#2


1  

What you're after is one (number or range) optionally followed by zero or more: comma plus (number or range):

您所追求的是一个(数字或范围)可选地后跟零或更多:逗号加(数字或范围):

(?:\d+(?:-\d+)?)(?:,(?:\d+(-\d+)?))*

This uses the \d shortcut for [0-9] and the (?: ... ) non-capturing parentheses construct.

这使用[0-9]的\ d快捷方式和(?:...)非捕获括号构造。

This regex doesn't allow any spaces to be included in the list. To allow those, insert the "optional space" \s* between each term:

此正则表达式不允许任何空格包含在列表中。要允许这些,请在每个术语之间插入“可选空格”\ s *:

\s*(?:\d+(\s*-\s*\d+)?)\s*(?:,\s*(?:\d+(\s*-\s*\d+)?)\s*)*

#3


0  

Lets say that CORE of your patter is

让我们说你的模式的核心是

([0-9]+.)|([0-9]+-[0-9]+)

It matches a single numeric value or a range. So, what you need is:

它匹配单个数值或范围。所以,你需要的是:

(CORE,)*CORE

And what you have is:

而你拥有的是:

(CORE,)*DIGIT

Do like this and you will be fine:

这样做你会没事的:

(([0-9]+.)|([0-9]+-[0-9]+),)*([0-9]+.)|([0-9]+-[0-9]+)

#4


0  

I think what you were looking for is:

我认为你在寻找的是:

(CORE)(,\s+CORE)*

This will allow lists that are:

这将允许以下列表:

CORE CORE, CORE CORE, CORE CORE, CORE, CORE

核心核心,核心核心,核心核心,核心

and so on.

等等。

Hope this helps,
David

希望这有帮助,大卫

#1


6  

You can use the regex;

你可以使用正则表达式;

^([0-9]+(-[0-9]+)?)(,([0-9]+(-[0-9]+)?))*$

Regex in action

正则表达式在行动

#2


1  

What you're after is one (number or range) optionally followed by zero or more: comma plus (number or range):

您所追求的是一个(数字或范围)可选地后跟零或更多:逗号加(数字或范围):

(?:\d+(?:-\d+)?)(?:,(?:\d+(-\d+)?))*

This uses the \d shortcut for [0-9] and the (?: ... ) non-capturing parentheses construct.

这使用[0-9]的\ d快捷方式和(?:...)非捕获括号构造。

This regex doesn't allow any spaces to be included in the list. To allow those, insert the "optional space" \s* between each term:

此正则表达式不允许任何空格包含在列表中。要允许这些,请在每个术语之间插入“可选空格”\ s *:

\s*(?:\d+(\s*-\s*\d+)?)\s*(?:,\s*(?:\d+(\s*-\s*\d+)?)\s*)*

#3


0  

Lets say that CORE of your patter is

让我们说你的模式的核心是

([0-9]+.)|([0-9]+-[0-9]+)

It matches a single numeric value or a range. So, what you need is:

它匹配单个数值或范围。所以,你需要的是:

(CORE,)*CORE

And what you have is:

而你拥有的是:

(CORE,)*DIGIT

Do like this and you will be fine:

这样做你会没事的:

(([0-9]+.)|([0-9]+-[0-9]+),)*([0-9]+.)|([0-9]+-[0-9]+)

#4


0  

I think what you were looking for is:

我认为你在寻找的是:

(CORE)(,\s+CORE)*

This will allow lists that are:

这将允许以下列表:

CORE CORE, CORE CORE, CORE CORE, CORE, CORE

核心核心,核心核心,核心核心,核心

and so on.

等等。

Hope this helps,
David

希望这有帮助,大卫