正则表达式,逗号分隔,单数,有序升序

时间:2022-10-01 00:20:07

I am attempting to write a regex to validate user input (asp.net, c#) which has the following conditions:

我正在尝试编写一个regex来验证具有以下条件的用户输入(asp.net, c#):

- single digits within a range of 1 - 6
- comma separated, but list should not begin or end with a comma
- digits cannot be repeated
- digits should be in ascending order

For example:

例如:

- 1,2,3,4,5,6   - valid
- 2,5,6         - valid
- 4             - valid
- 2,5,6,        - invalid
- 3,6,5         - invalid
- 2,2,5,6       - invalid

So far I've got:

到目前为止我有:

^((1,)?(2,)?(3,)?(4,)?(5,)?(6)?)$

The issue with this is the numbers 1-5 have to be followed by a comma which, if they are the only number being input, is not correct.

问题是,数字1-5必须后面跟着一个逗号,如果它们是唯一输入的数字,是不正确的。

4 个解决方案

#1


2  

You can use \b to ensure that you are at the boundary of the word, and ,* to have a comma OR no comma. This results in the working - albeit quite long

您可以使用\b来确保您位于单词的边界,并且,*具有逗号或无逗号。这导致了工作——尽管时间很长。

^((1)?(\b,?2)?(\b,?3)?(\b,?4)?(\b,?5)?(\b,?6)?)$

#2


0  

(This is a silly answer)

(这是一个愚蠢的回答)

Given that there are six values, and each can be either present or not present, there are 2^6 = 64 possible correct values; except that I'm guessing we want to exclude the possibility of no numbers at all being present, so there are only 63 possible correct values. This regex allows them and only them:

鉴于有六个值,每个可以在场或不在场,可能有2 ^ 6 = 64正确的价值观;除了我猜我们想排除没有数字存在的可能性,所以只有63个可能的正确值。本regex只允许它们:

^(6|5|5,6|4|4,6|4,5|4,5,6|3|3,6|3,5|3,5,6|3,4|3,4,6|3,4,5|3,4,5,6|2|2,6|2,5|2,5,6|2,4|2,4,6|2,4,5|2,4,5,6|2,3|2,3,6|2,3,5|2,3,5,6|2,3,4|2,3,4,6|2,3,4,5|2,3,4,5,6|1|1,6|1,5|1,5,6|1,4|1,4,6|1,4,5|1,4,5,6|1,3|1,3,6|1,3,5|1,3,5,6|1,3,4|1,3,4,6|1,3,4,5|1,3,4,5,6|1,2|1,2,6|1,2,5|1,2,5,6|1,2,4|1,2,4,6|1,2,4,5|1,2,4,5,6|1,2,3|1,2,3,6|1,2,3,5|1,2,3,5,6|1,2,3,4|1,2,3,4,6|1,2,3,4,5|1,2,3,4,5,6)$

Please don't actually use this. You will make us both look bad.

请不要用这个。你会让我们俩都变坏的。

#3


0  

Non-regex version. Simple and precise

Non-regex版本。简单和精确

string str = ",1,  2,3, 4, 5, 6";
bool valid = false;
var invalidString = str.Split(',').Any(p =>
{
    int num = 0;
    return int.TryParse(p, out num);
});
if (!invalidString)
{
    List<int> list = str.Split(',').Select(p => int.Parse(p)).ToList();
    var sorted = list.SequenceEqual(list.OrderBy(p => p));
    var hasDuplicates = list.Count != list.Distinct().Count();
    valid = sorted && !hasDuplicates;
}

#4


0  

Strictly for learning purposes, break the problem down into pieces.

严格地为了学习目的,把问题分解成几个部分。

It must be formed as a digit followed by zero or more comma plus digits.

它必须以数字的形式,后跟零或更多的逗号加数字。

^\d(?:,\d)*$

There are only 6 digits and they must be in ascending order. So just list them and their intervening commas; each of which is optional.

只有6位数字,它们必须是按升序排列的。列出它们和它们之间的逗号;每一个都是可选的。

^1?,?2?,?3?,?4?,?5?,?6?$

The difficulty is that both of the above regular expressions must match at the same time. We can use a zero width look aheads on one of them. This will do the match but will not "consume" any characters. Hence after it matches the next piece of the regular expression will start at the same place as the look ahead. The look ahead is achieved by wrapping an expression in (?= and ')'. Giving:

难点在于上述两个正则表达式必须同时匹配。我们可以在其中一个上用0的宽度。这将进行匹配,但不会“消耗”任何字符。因此,在它匹配下一个正则表达式之后,将在前面的前面开始。通过将表达式封装在(?=和‘)’。给:

(?=^\d(?:,\d)*$)

Combining the two regular expressions gives the following:

结合这两个正则表达式可以得出以下结论:

(?=^\d(?:,\d)*$)^1?,?2?,?3?,?4?,?5?,?6?$

#1


2  

You can use \b to ensure that you are at the boundary of the word, and ,* to have a comma OR no comma. This results in the working - albeit quite long

您可以使用\b来确保您位于单词的边界,并且,*具有逗号或无逗号。这导致了工作——尽管时间很长。

^((1)?(\b,?2)?(\b,?3)?(\b,?4)?(\b,?5)?(\b,?6)?)$

#2


0  

(This is a silly answer)

(这是一个愚蠢的回答)

Given that there are six values, and each can be either present or not present, there are 2^6 = 64 possible correct values; except that I'm guessing we want to exclude the possibility of no numbers at all being present, so there are only 63 possible correct values. This regex allows them and only them:

鉴于有六个值,每个可以在场或不在场,可能有2 ^ 6 = 64正确的价值观;除了我猜我们想排除没有数字存在的可能性,所以只有63个可能的正确值。本regex只允许它们:

^(6|5|5,6|4|4,6|4,5|4,5,6|3|3,6|3,5|3,5,6|3,4|3,4,6|3,4,5|3,4,5,6|2|2,6|2,5|2,5,6|2,4|2,4,6|2,4,5|2,4,5,6|2,3|2,3,6|2,3,5|2,3,5,6|2,3,4|2,3,4,6|2,3,4,5|2,3,4,5,6|1|1,6|1,5|1,5,6|1,4|1,4,6|1,4,5|1,4,5,6|1,3|1,3,6|1,3,5|1,3,5,6|1,3,4|1,3,4,6|1,3,4,5|1,3,4,5,6|1,2|1,2,6|1,2,5|1,2,5,6|1,2,4|1,2,4,6|1,2,4,5|1,2,4,5,6|1,2,3|1,2,3,6|1,2,3,5|1,2,3,5,6|1,2,3,4|1,2,3,4,6|1,2,3,4,5|1,2,3,4,5,6)$

Please don't actually use this. You will make us both look bad.

请不要用这个。你会让我们俩都变坏的。

#3


0  

Non-regex version. Simple and precise

Non-regex版本。简单和精确

string str = ",1,  2,3, 4, 5, 6";
bool valid = false;
var invalidString = str.Split(',').Any(p =>
{
    int num = 0;
    return int.TryParse(p, out num);
});
if (!invalidString)
{
    List<int> list = str.Split(',').Select(p => int.Parse(p)).ToList();
    var sorted = list.SequenceEqual(list.OrderBy(p => p));
    var hasDuplicates = list.Count != list.Distinct().Count();
    valid = sorted && !hasDuplicates;
}

#4


0  

Strictly for learning purposes, break the problem down into pieces.

严格地为了学习目的,把问题分解成几个部分。

It must be formed as a digit followed by zero or more comma plus digits.

它必须以数字的形式,后跟零或更多的逗号加数字。

^\d(?:,\d)*$

There are only 6 digits and they must be in ascending order. So just list them and their intervening commas; each of which is optional.

只有6位数字,它们必须是按升序排列的。列出它们和它们之间的逗号;每一个都是可选的。

^1?,?2?,?3?,?4?,?5?,?6?$

The difficulty is that both of the above regular expressions must match at the same time. We can use a zero width look aheads on one of them. This will do the match but will not "consume" any characters. Hence after it matches the next piece of the regular expression will start at the same place as the look ahead. The look ahead is achieved by wrapping an expression in (?= and ')'. Giving:

难点在于上述两个正则表达式必须同时匹配。我们可以在其中一个上用0的宽度。这将进行匹配,但不会“消耗”任何字符。因此,在它匹配下一个正则表达式之后,将在前面的前面开始。通过将表达式封装在(?=和‘)’。给:

(?=^\d(?:,\d)*$)

Combining the two regular expressions gives the following:

结合这两个正则表达式可以得出以下结论:

(?=^\d(?:,\d)*$)^1?,?2?,?3?,?4?,?5?,?6?$