I have an XML property that has two possible value: discardOld and discardNew (not case sensitive).
I’m trying to come up with a RegEx to validate the input for that particular property:
I tried this one: (?i)(discardnew|discardold) but it doesn’t seem to work.
Any help would be greatly appreciated! Thanks!
UPDATE
This is the code I use to perform the RegEx check:
[ConfigurationProperty("mode", IsRequired = true)]
[RegexStringValidator("(?i)(discardnew|discardold)")]
public string Mode
{
get { return (string)base["mode"]; }
set { base["mode"] = value; }
}
This is how the property is declared in XML:
<eventQueue mode="discardNew" />
I tried this and it seems to work correctly (all following IsMatch returns true):
Problem is probably somewhere else.
UPDATE:
This works forr me:
I just added DefaultValue = “discardnew” to ConfigurationProperty. ASP.NET always checks DefaultValue against RegEx even if you don’t set it. Then it considers it to be empty string, which doesn’t validate against your regular expression and so it throws Exception). Also consider addind ^ and $ to regular pattern, otherwise it will validate not only discardnew, but also discardnew1, 1discardnew, etc…