简体   繁体   中英

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, @"^([^,](,[^,])*)?$");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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