How do I count multiple placeholders in a String in C#?

Given is a string str 'Hello <name1>, hello <name2> and hello <name3>'

The <nameX>s are placeholders. How do I count the amount of different placeholders in this string?


If you want a more generic solution that handles placeholders starting from any number, with possible gaps inbetween, it's trivially easy with regex:

Regex.Matches(s, @"<name\d+>").Count

That will return you the number of any placeholders. It's also easy to filter it by distinct placeholders:

Regex.Matches(s, @"<name\d+>").Select(m => m.Value).Distinct().Count()