You need to create a Regex
for each of the patterns you want to recognize, then match the strings with each Regex
until you have a match or until you have tried all Regex
es.
public bool TryParseDate(string input, out DateTime result)
{
if (MatchPattern1(input, result))
{
return true;
}
if (MatchPattern2(input, result))
{
return true;
}
...
return false;
}
where the MatchPattern
methods each look for a particular Regex:
private bool MatchPattern1(string input, out DateTime result)
{
Match match = Regex.Match(input, @"*pattern here*");
if (match.Success)
{
result = *build date based on matches*;
return true;
}
return false;
}
This way, you can do your matching however you want (with or without regular expressions) and make them as complicated as you want.