[C#] Extracting a Single Match from a String Using Regex Pattern (Regex.Match)

To find a specific pattern (such as an order number, ID, or amount) within a string and retrieve only the first occurrence, use the Regex.Match method.

Also, by using the “Grouping ()” feature of regular expressions, you can extract just the “numeric part of the ID” instead of the “entire order ID.”

目次

Table of Contents

  • Implementation Sample: Extracting an Order Number
  • Sample Code
  • Execution Result
  • Explanation and Technical Points
    1. Match Object
    2. Groups Property (Partial Extraction)

Implementation Sample: Extracting an Order Number

In the following code, I search for an “Order Number (Order-XXXX)” within an email subject line and extract only that number part.

Sample Code

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        // String to analyze (e.g., email subject)
        string subject = "Re: [Order-7721] Regarding shipping status";

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

        // ---------------------------------------------------------
        // Pattern 1: Get the matched part as is
        // ---------------------------------------------------------
        // Meaning of @"Order-\d+":
        // Order- : The literal text "Order-"
        // \d+    : One or more digits
        Match m1 = Regex.Match(subject, @"Order-\d+");

        if (m1.Success)
        {
            // The Value property contains the entire matched string
            Console.WriteLine($"Pattern 1 Detected: {m1.Value}");
        }

        Console.WriteLine("---");

        // ---------------------------------------------------------
        // Pattern 2: Use parentheses () to extract only the number part
        // ---------------------------------------------------------
        // Meaning of @"Order-(\d+)":
        // Order- : The text "Order-"
        // (\d+)  : Group and capture the number part
        Match m2 = Regex.Match(subject, @"Order-(\d+)");

        if (m2.Success)
        {
            // Groups[0] is the entire match ("Order-7721")
            Console.WriteLine($"Entire Match   : {m2.Groups[0].Value}");

            // Groups[1] is the content of the first () ("7721")
            // This extracts "only the numbers"
            Console.WriteLine($"Numbers Only   : {m2.Groups[1].Value}");
        }
    }
}

Execution Result

Target: Re: [Order-7721] Regarding shipping status

Pattern 1 Detected: Order-7721
---
Entire Match   : Order-7721
Numbers Only   : 7721

Explanation and Technical Points

1. Match Object

When you execute Regex.Match, it returns data as a Match class object.

  • Success: Indicates whether the pattern was found (true/false). Always check this before retrieving values.
  • Value: Contains the entire string that matched the regular expression.

2. Groups Property (Partial Extraction)

By using parentheses ( ) inside a regular expression, you can save that specific part individually as a “Group.” This is stored in the Groups collection.

  • Groups[0]: The entire match (same content as Value).
  • Groups[1]: The content matched by the first ( ).
  • Groups[2]: The content of the second ( ) (if it exists).

Using this, you can easily write logic to extract just the “Value” from a “Key=Value” format, or extract just the “ID number without the prefix” as shown in this example.

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

この記事を書いた人

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

目次