I have a string which based on a regex matching is of format_1 or format_2 etc. I use regex named groups to extract clientId variable.
var sampleName = "001-99-26Jul2023";
var sampleNameFormat1 = @"[0-9]{3}-(?<clientId>[0-9]{2})-[0-9]{2}[a-zA-Z]{3}[0-9]{4}";
var sampleNameFormat2 = @"[0-9]{4}-(?<clientId>[0-9]{2})-[0-9]{2}[a-zA-Z]{3}[0-9]{4}";
var sampleNameFormat3 = @"[0-9]{5}-[0-9]{2}[a-zA-Z]{3}[0-9]{4}";
var regexMatch = sampleName switch
{
var _ when Regex.IsMatch(sampleName, sampleNameFormat1, RegexOptions.IgnoreCase) => Regex.Match(sampleName, sampleNameFormat1, RegexOptions.IgnoreCase),
var _ when Regex.IsMatch(sampleName, sampleNameFormat2, RegexOptions.IgnoreCase) => Regex.Match(sampleName, sampleNameFormat2, RegexOptions.IgnoreCase),
var _ when Regex.IsMatch(sampleName, sampleNameFormat3, RegexOptions.IgnoreCase) => Regex.Match(sampleName, sampleNameFormat3, RegexOptions.IgnoreCase),
_ => null,
};
var clientIdString = regexMatch.Result("${clientId}");
if(!string.IsNullOrEmpty(clientIdString)) // If check, because `sampleNameFormat3` does not have this named grouping <clientId>
{
int clientId = ParseInteger(clientIdString);
}
Problems:
- I do the regex match call twice. Is there a syntax that makes it more clean? Some way to change the switch statement? Only one Regex.Match call.
- As you see in Format 3 there is no
<clientId>, so the last assignment
var clientIdString = regexMatch.Result("${clientId}");will result inclientIdString = "${clientId}".
So, the if check will enter and errorParseInteger("${clientId}").
The solution would be to write:var clientIdString = regexMatch.Groups["clientId"].Value;this assigns empty string and if-check does not enter. I don't know if this is the right way, so I wanted to ask you too what you consider.
The switch is overkill, and doesn't really add much in terms of readability or succinctness.
Just do
If you add some number of cases, then I suggest looping over the calls as you and @madreflection suggest in the comments. Something like
or