[C#] Retrieving “All” Occurrences Matching a Pattern in a String (Regex.Matches)

While Regex.Match retrieves only the “first” match, the Regex.Matches method allows you to retrieve all matching occurrences within a string as a collection (list).

This method is essential when you want to extract multiple pieces of data, such as extracting data from log files or analyzing tags within text.

目次

Table of Contents

  • Implementation of Extracting All Matches
  • Sample Code
  • Execution Result
  • Explanation and Technical Points
    1. MatchCollection Class
    2. Utilizing Grouping
    3. Performance Considerations

Implementation of Extracting All Matches

In the following sample code, I search for and list all items matching the “Product ID (1 alphabet letter – 3 digits)” pattern from a product list string.

Sample Code

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        // Data to analyze (string containing multiple IDs)
        string data = "Stock Check: Item [A-101], Item [B-205], Item [Z-999] (3 items total)";

        Console.WriteLine($"Target Data: {data}\n");

        // ---------------------------------------------------------
        // Pattern 1: Extract all parts that meet the condition
        // ---------------------------------------------------------
        // [A-Z] : One uppercase letter
        // -     : Hyphen
        // \d{3} : 3 digits
        // Get all occurrences matching the above
        MatchCollection matches1 = Regex.Matches(data, @"[A-Z]-\d{3}");

        Console.WriteLine($"--- Simple Extraction (Count: {matches1.Count}) ---");
        
        // MatchCollection can be looped with foreach
        foreach (Match m in matches1)
        {
            Console.WriteLine($"ID Found: {m.Value}");
        }

        Console.WriteLine();

        // ---------------------------------------------------------
        // Pattern 2: Use grouping () to extract only the "number part" inside the ID
        // ---------------------------------------------------------
        // Item\s+\[      : "Item" + whitespace + "["
        // ([A-Z]-\d{3})  : (Group 1) Capture the entire ID
        // \]             : "]"
        MatchCollection matches2 = Regex.Matches(data, @"Item\s+\[([A-Z]-\d{3})\]");

        Console.WriteLine("--- Group Extraction ---");

        foreach (Match m in matches2)
        {
            // Get only the content of () using Groups[1]
            // Here we display only the ID, excluding the Item [...] frame
            Console.WriteLine($"Content only: {m.Groups[1].Value}");
        }
    }
}

Execution Result

Target Data: Stock Check: Item [A-101], Item [B-205], Item [Z-999] (3 items total)

--- Simple Extraction (Count: 3) ---
ID Found: A-101
ID Found: B-205
ID Found: Z-999

--- Group Extraction ---
Content only: A-101
Content only: B-205
Content only: Z-999

Explanation and Technical Points

1. MatchCollection Class

The return value of Regex.Matches is of type MatchCollection. This is a collection (similar to a list) of Match objects and has the following features:

  • Loopable: You can process all matches sequentially using foreach.
  • Count Property: You can check the total number of matches.
  • Index Access: You can access items like an array (e.g., matches[0]).

2. Utilizing Grouping

Just like with a single Match, the Groups property is valid within each Match object retrieved by Matches.

This enables advanced extraction processing, such as “finding all lines matching a pattern (Matches) and then extracting only specific values from those lines (Groups).”

3. Performance Considerations

Executing Matches with complex regular expressions on very long strings may take time. If you are analyzing text larger than several megabytes, consider using overload methods that allow specifying a timeout duration.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次