I have a string and multiple regex’s, for example a regex that checks if the string is a number only, if it starts with character X and so on. I have different codes running depending on what regex gets matched, like this:
if (Regex.IsMatch(myString, regex1))
{
//number
}
else if (Regex.IsMatch(myString, regex2))
{
//something else
}
else if (Regex.IsMatch(myString, regex3))
{
//something else
}
and so on. However, this looks very clunky as I have like 10 regex’s to go through, so can I do the same thing using switch/case? If so, can I be provided with an example?
I am using .NET 2.0 and WinForms.
This cannot be done as you describe because switch can only be used with: bool, char string, int, enum or a corresponding nullable type.