I need to dynamically build a Regex to catch the given keywords, like
string regex = "(some|predefined|words";
foreach (Product product in products)
regex += "|" + product.Name; // Need to encode product.Name because it can include special characters.
regex += ")";
Is there some kind of Regex.Encode that does this?
You can use
Regex.Escape. For example:Output:
Or without the LINQ, but using string concatenation in a loop (which I try to avoid) as per your original code: