diff --git a/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs b/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs index 75769bf172996..5876aad542123 100644 --- a/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs +++ b/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs @@ -58,14 +58,18 @@ internal partial class ReadOnlyCollections // // - private static Dictionary CountWords(ReadOnlySpan input) + static Dictionary CountWords(ReadOnlySpan input) { Dictionary wordCounts = new(StringComparer.OrdinalIgnoreCase); Dictionary.AlternateLookup> spanLookup = wordCounts.GetAlternateLookup>(); - foreach (Range wordRange in Regex.EnumerateSplits(input, @"\b\w+\b")) + foreach (Range wordRange in Regex.EnumerateSplits(input, @"\b\W+")) { + if (wordRange.Start.Value == wordRange.End.Value) + { + continue; // Skip empty ranges. + } ReadOnlySpan word = input[wordRange]; spanLookup[word] = spanLookup.TryGetValue(word, out int count) ? count + 1 : 1; }