c# - 正则表达式逗号分隔单个数字升序排列

标签 c# regex csv digit

我正在尝试编写一个正则表达式来验证用户输入(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

例如:

- 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

到目前为止我已经得到:

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

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

最佳答案

您可以使用 \b 确保位于单词的边界,并使用 ,* 来确保有逗号或没有逗号。这导致工作 - 尽管很长

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

关于c# - 正则表达式逗号分隔单个数字升序排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207553/

相关文章:

python - python 问题中典型的绵羊计数正则表达式

perl - 使用 Perl 解析 CSV 文件从特定行到文件末尾

c# - Lucene.Net 多字段多词搜索和通配符使用和短语搜索,模糊搜索都

c# - Convert.FromBase64String c# 上的 FormatException

regex - 查找具有确切字符数的单词

python - 编写 CSV 时添加的额外字符

python - 使用 python 和 pandas 按季节分组数据

C# 代码更改未通过 IIS 反射(reflect)

c# - 在运行时清除 asp.net 窗体

javascript - jQuery/正则表达式 : remove all p tags