I have a regular expression in my code to match on the keys (control ClientId) within a form POST. I am looping round all the keys in the form data and performing the required action when a match is found.
However the regex is matching more times than I need it to. This runs on every request and if performing unnecessary code to fire.
Match match = Regex.Match(key.ToLower(), @"(?!\$)(?:[a-z0-9]+)$",
RegexOptions.Compiled);
Sample string that are coreectly matching
master$maincontentplaceholder$ucsearchresults$hdnvalue
master$maincontentplaceholder$ucsearchresults$hdnvalue2
master$maincontentplaceholder$ucsearchresults$hdnvalue3
I then have a switch statment with cases matching the control id
case: "hdnvalue"
case: "hdnvalue2"
case: "hdnvalue3"
The form is also returning a large number of extra keys that do not always require processing. It would be good if I could exclude these from being matched within the regex. (note the extra client id level $ucfilter)
master$maincontentplaceholder$ucsearchresults$ucfilter$hdnvalue
master$maincontentplaceholder$ucsearchresults$ucfilter$hdnvalue2
master$maincontentplaceholder$ucsearchresults$ucfilter$hdnvalue3
If you don’t /have/ to use regex, you might do better with LastIndexOf(“$”)