C # ASP.NET Application Regular Expression

For C # ASP.NET application - This should be simple, but I can't figure out this regex, I need a list of strings that the textbox cannot represent as a value, and I have to ignore the case.

Example - Regardless of capitalization, I need my regex to reject the following lines: abc, def, ghi

I can't even get the regex to reject one of them. I tried the following:

[RegularExpression(@"(\W|^)(?i)!ABC(?-i)(\W|$)", ErrorMessage = "REJECTED!")]
public string Letters { get; set; }

      

This does not work! It seems to reject everything. Does anyone know what it should look like? How can I give up all of them?

Thanks for any help you can provide!

+3


source to share


6 answers


Quick and dirty, but try it (if I understood the problem correctly!)



^(?i)(?!(ABC|DEF|GHI)(?-i)).*$

      

+2


source


If you only want to ignore lines then use this

^(?i)(?!.*(?:abc|def|ghi))

      



If you want to ignore words, use word boundaries around the pattern

^(?i)(?!.*\b(?:abc|def|ghi)\b)

      

+2


source


In standard regex syntax this would be ^(?!abc$)(?!def$).*

+2


source


This will detect abc, def and ghi

(?i)(abc|def|ghi)

      

enclose in ^

and $

only match with them and nothing else (for example will not match wxabcyz)

^(?i)(abc|def|ghi)$

      

Finally, if you want to match something like "This is some kind of random abc text" and reject it, do this

(?i)\b(abc|def|ghi)\b

      

+2


source


^((?!abc)|(?!def)|(?!ghi).)*$ 
      

That's about it.

Btw I would recommend that you play around with something like the following resources that you are not already.

regex pal and regular-expressions.info

+1


source


^(?i)(?!(ABC|DEF|GHI)(?-i)).*$

      

0


source







All Articles