Skip to main content

How to Make a C# Regex Ignore Case

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Regular expressions are case-sensitive: ^password: doesn't match Password:. Pass RegexOptions.IgnoreCase and the pattern matches whatever the case.

Microsoft's reference describes the option as Specifies case-insensitive matching, and adds an important detail: By default, the casing conventions of the current culture are used. For text that isn't shown to people, such as keys, codes or field names, pair it with RegexOptions.CultureInvariant.

Syntax​

Syntax
Regex.IsMatch(<text>, <pattern>, RegexOptions.IgnoreCase)
Regex.IsMatch(<text>, <pattern>, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)
Regex.IsMatch(<text>, "(?i)<pattern>")
PartWhat it is
RegexOptions.IgnoreCaseMatches upper and lower case alike
RegexOptions.CultureInvariantUses the same casing rules on every computer instead of the current culture's
(?i) at the start of the patternTurns on case-insensitive matching from inside the pattern

The second line combines both options with the | operator.

The Example​

A console app called IgnoreCase checks four lines for password: at their start, once with exact case and once with any case.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n IgnoreCase
cd IgnoreCase
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/IgnoreCase/IgnoreCase.csproj:
Determining projects to restore...
Restored /Users/you/source/IgnoreCase/IgnoreCase.csproj (in 47 ms).
Restore succeeded.

dotnet new console -n IgnoreCase creates a folder named IgnoreCase with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or IgnoreCase.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the lines:

Program.cs (the lines)
using System.Text.RegularExpressions;

string[] lines = ["Password: x", "PASSWORD: y", "password: z", "Passcode: w"];

Then the two checks:

Program.cs (the checks)
foreach (string line in lines)
{
bool exactCase = Regex.IsMatch(line, "^password:");
bool anyCase = Regex.IsMatch(line, "^password:", RegexOptions.IgnoreCase);
Console.WriteLine($"{line,-12} exact case: {exactCase,-5} any case: {anyCase}");
}
  • Regex.IsMatch(line, "^password:") needs the exact case: ^ is the start of the line.
  • RegexOptions.IgnoreCase as the third argument makes the same pattern match any case.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Password: x exact case: False any case: True
PASSWORD: y exact case: False any case: True
password: z exact case: True any case: True
Passcode: w exact case: False any case: False

macOS-style terminal window titled Terminal — IgnoreCase showing dotnet run: with exact case only password: matches, with any case Password:, PASSWORD: and password: all match, and Passcode: matches neither way

With exact case, only password: z matched. With IgnoreCase, so did Password: x and PASSWORD: y. Passcode: w is a different word, so it matched neither way.

The Complete File​

macOS-style code window titled Program.cs — IgnoreCase showing four lines starting with password in different cases or passcode, and a loop that checks each line with Regex.IsMatch once without options and once with RegexOptions.IgnoreCase

Program.cs
using System.Text.RegularExpressions;

string[] lines = ["Password: x", "PASSWORD: y", "password: z", "Passcode: w"];

foreach (string line in lines)
{
bool exactCase = Regex.IsMatch(line, "^password:");
bool anyCase = Regex.IsMatch(line, "^password:", RegexOptions.IgnoreCase);
Console.WriteLine($"{line,-12} exact case: {exactCase,-5} any case: {anyCase}");
}

Common Mistakes​

MistakeWhat happensDo this instead
Relying on IgnoreCase alone for text that isn't shown to peopleIt follows the current culture. With the culture set to Turkish, Regex.IsMatch("I", "i", RegexOptions.IgnoreCase) returned FalseAdd RegexOptions.CultureInvariant: the same check then returned True
Expecting [a-z] to stay lowercase-onlyWith IgnoreCase, ^[a-z]+$ matched ABCLeave IgnoreCase off where the case matters

FAQ​

Can I turn it on inside the pattern?​

Yes: (?i)^password: matched PASSWORD: y. CultureInvariant can't be set that way. The options reference: It is not available as an inline option.

How do I combine options?​

With |. RegexOptions.IgnoreCase | RegexOptions.CultureInvariant printed as IgnoreCase, CultureInvariant: the reference says the enumeration supports a bitwise combination of its member values.

Conclusion​

RegexOptions.IgnoreCase makes a regular expression ignore case, using the current culture's rules unless you add RegexOptions.CultureInvariant. Use both together for anything that has to behave the same on every computer.

Additional Resources​