I want to write a code that checks if it matches to a regular expression then prints matched string.
I found other question Can I use regex expression in c# with switch case?, but IsMatch method only returns boolean so I couldn't retrieve named group string.
var filepath = @"path/to/file.txt";
foreach (string line File.ReadLines(filepath))
{
switch (line)
{
case var s when new Regex(@"^(?<UserName>\w+) moved to (?<Position>\w+)$").IsMatch(s):
// I have to know <UserName> and <Position> here.
break;
...
}
}