I have a bunch of SKUs (stock keeping units) that represent a series of strings that I\'d like to create a single Regex to match for.
So, for example, if I have SKUs
This works if each SKU id have the same length.
// ...
string regexStr = Calculate(skus);
// ...
public static string Calculate(IEnumerable rest) {
if (rest.First().Length > 0) {
string[] groups = rest.GroupBy(r => r[0])
.Select(g => g.Key + Calculate(g.Select(e => e.Substring(1))))
.ToArray();
return groups.Length > 1 ? "(" + string.Join("|", groups) + ")" : groups[0];
} else {
return string.Empty;
}
}