I have the following code
var definitionMap = definitions.Aggregate("", (current, next) =>
current + "[\\W\\s]" +
next.Tag.Replace("(", "\\(").Replace(")", "\\)") + "[\\W\\s]" + "|");
This works pretty well except I get a trailing | on the end, I could just remove this manually but is there anyway to stop the aggregate method from putting it there in the first place
You could add it conditionally at the start of each term, i.e.
However, I would also suggest you look at
StringBuilderand aforeachloop here instead ofAggregate. Actually, sinceStringBuilderhas a fluent API you can actually use it insideAggregate– but I don’t recommend it. Just because you can use a LINQ extension method doesn’t mean it is automatically either clearer or better. In this case, it is neither IMO. In particular, at the moment you are generating a lot of unnecessary intermediate strings.