3

For a C#, ASP.NET application - This should be simple, but I can't figure out this regular expression, I need a list of strings that a text box can not submit as a value, and I have to ignore the case.

Example - No matter the capitalization, I need my regular expression to reject the following strings - abc, def, ghi

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

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

That does not work! It seems to reject everything. Anyone know what it should look like? How can I reject all of them?

Thanks for any help can provide!

6 Answers 6

2

Quick and dirty, but give this a try (assuming I understand the problem correctly!)

^(?i)(?!(ABC|DEF|GHI)(?-i)).*$
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

This will detect abc, def, and ghi

(?i)(abc|def|ghi)

enclose in ^ and $ to only match those and nothing else (e.g. won't match wxabcyz)

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

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

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

Comments

2

If you want to ignore just strings, use this

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

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

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

Comments

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

Thats about it.

Btw I'd recommend you play around with something like the following resources you aren't already.

regex pal and regular-expressions.info

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.