[C#] Performing Case-Insensitive Regex Searches (RegexOptions.IgnoreCase)

By default, C# Regular Expressions (Regex) strictly distinguish between uppercase and lowercase letters (e.g., “Apple” and “apple” are treated as different).

If you want to allow for variations in user input and match text regardless of case, you should specify the RegexOptions.IgnoreCase option.

目次

Table of Contents

  • Implementation Example: SQL Keyword Search
  • Sample Code
  • Execution Result
  • Explanation and Technical Points
    1. RegexOptions.IgnoreCase
    2. Inline Option (?i)

Implementation Example: SQL Keyword Search

In the following sample code, I extract reserved words (such as SELECT, FROM, WHERE) contained in a SQL statement, regardless of whether they are written in uppercase, lowercase, or a mix of both.

Sample Code

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        // Target string (SQL query)
        // Contains Uppercase (SELECT), Lowercase (where), and Mixed (From)
        string sqlQuery = "SELECT id, name From users where active = 1 AND role = 'admin'";

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

        // Keyword pattern to search
        // \b : Word boundary
        string pattern = @"\b(select|from|where|and)\b";

        // ---------------------------------------------------------
        // Specify RegexOptions.IgnoreCase in the 3rd argument
        // This allows matching "SELECT" even with the pattern "select"
        // ---------------------------------------------------------
        MatchCollection matches = Regex.Matches(sqlQuery, pattern, RegexOptions.IgnoreCase);

        Console.WriteLine($"--- Found Keywords (Total: {matches.Count}) ---");

        foreach (Match m in matches)
        {
            // m.Value : The actually matched string (original text)
            // m.Index : Position found
            Console.WriteLine($"Pos {m.Index, 2}: {m.Value}");
        }
    }
}

Execution Result

Target Query: SELECT id, name From users where active = 1 AND role = 'admin'

--- Found Keywords (Total: 4) ---
Pos  0: SELECT
Pos 16: From
Pos 27: where
Pos 44: AND

Explanation and Technical Points

1. RegexOptions.IgnoreCase

Methods such as Regex.Matches and Regex.IsMatch have overloads that accept the RegexOptions enum as the third (or second) argument. By passing RegexOptions.IgnoreCase here, the engine treats letters like ‘A’ and ‘a’ as equivalent.

2. Inline Option (?i)

Instead of specifying the option in the C# code, there is also a syntax to embed the option directly into the regular expression pattern. If you place (?i) at the beginning of the pattern, the rest of the pattern will become case-insensitive.

// Pattern that ignores case without specifying options in code
var matches = Regex.Matches(text, @"(?i)\b(select|from)\b");

This method is effective when you cannot pass RegexOptions as an argument due to library limitations, or when you want to define the behavior completely within the pattern string itself.

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

この記事を書いた人

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

目次