简体   繁体   English

使用逗号验证字符的正则表达式

[英]Regular expression to validate chars by comma

It should validate something like this: 它应该验证如下内容:

a,b,c,d,1,2,3,w,f,x,-,=,d

Accepted only one char and next is comma. 仅接受一个字符,下一个是逗号。

Bad example: 错误的例子:

adc,1,2,345,flos 

I think of thsi: 我认为:

@"([*{1}]+[,{1}])+"

but this not work. 但这行不通。

Try ^[^,](,[^,])*$ . 尝试^[^,](,[^,])*$ The structure is as follows: 结构如下:

  1. a single non comma character: [^,] 单个非逗号字符: [^,]
  2. arbitrary many times a comma followed by a non comma character: (,[^,])* 任意多次逗号后跟非逗号字符: (,[^,])*

If you want to match an empty string as well you should make the whole expression optional: 如果还想匹配一个空字符串,则应使整个表达式为可选:

^([^,](,[^,])*)?$

如果您想char with comma从允许的char with comma找到任何组,简单的方法是

(a|b|c|d|1|2|3|w|f|x|\-|\=)\,

It gives you what you want i guess : 它给你你想要的我猜:

bool isMatched = IsValid("adc,1,2,345,flos");   

    private bool IsValid(string value)
    {
            return Regex.IsMatch(value, @"^([^,](,[^,])*)?$");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM