[C#] Specifying Multiple Regex Options Simultaneously (Combining RegexOptions)

When using regular expressions, you might want to enable “Case Insensitive (IgnoreCase)” and “Multiline Mode (Multiline)” at the same time.

Since RegexOptions in C# is defined as bit flags, you can combine multiple options using the bitwise OR operator (|).

目次

Table of Contents

  • Implementation Example of Multiple Options
  • Sample Code
  • Execution Result
  • Explanation and Technical Points
    1. Combining Options (| Operator)
    2. RegexOptions.Multiline
    3. RegexOptions.Singleline (Note)

Implementation Example of Multiple Options

In the following sample code, I extract values from lines starting with Status: from system state logs (multi-line strings). Since the logging format is inconsistent and mixes uppercase and lowercase, I apply two options simultaneously to parse it.

Sample Code

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        // Text to analyze
        // Status is recorded per line, but written inconsistently as "Status", "STATUS", etc.
        string logData = "Start process...\n" +
                         "Status: Ready\n" +
                         "Processing data...\n" +
                         "STATUS: Error\n" +
                         "End process.";

        Console.WriteLine("--- Data to Analyze ---");
        Console.WriteLine(logData);
        Console.WriteLine("----------------------\n");

        // Regular Expression Pattern
        // ^       : Start of line (Matches the start of each line thanks to Multiline)
        // status: : String "status:"
        // \s* : Whitespace
        // (\w+)   : Capture word characters (Ready, Error, etc.)
        string pattern = @"^status:\s*(\w+)";

        // ---------------------------------------------------------
        // Combining options
        // Use | (bitwise OR) to connect multiple options
        // 1. IgnoreCase : Ignore case of "status"
        // 2. Multiline  : Make ^ match "start of each line" instead of "start of string"
        // ---------------------------------------------------------
        var options = RegexOptions.IgnoreCase | RegexOptions.Multiline;

        MatchCollection matches = Regex.Matches(logData, pattern, options);

        Console.WriteLine($"Found: {matches.Count} items");

        foreach (Match m in matches)
        {
            // Extract only the status value with Groups[1]
            Console.WriteLine($"Status Detected: {m.Groups[1].Value}");
        }
    }
}

Execution Result

--- Data to Analyze ---
Start process...
Status: Ready
Processing data...
STATUS: Error
End process.
----------------------

Found: 2 items
Status Detected: Ready
Status Detected: Error

Explanation and Technical Points

1. Combining Options (| Operator)

RegexOptions is an enumeration with the [Flags] attribute. Therefore, you can chain as many options as needed using the | (pipe) symbol.

// Example: Ignore case, AND multiline, AND compiled for speed
var ops = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled;

2. RegexOptions.Multiline

Normally, the regex anchors ^ (start) and $ (end) match only the very beginning and end of the entire string.

When this option is specified, the behavior of ^ and $ changes to match the start and end of each line separated by newlines (\n). This is an essential option when parsing line-based data like log files.

3. RegexOptions.Singleline (Note)

There is also an option called RegexOptions.Singleline. Although the name sounds similar, it is not the opposite of Multiline.

  • Multiline: ^ and $ match each line.
  • Singleline: . (dot) matches newline characters as well (treating the whole string as one long line).

Please be careful, as these names can be confusing.

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

この記事を書いた人

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

目次