C# RegexOptions.IgnoreCase Example

This C# program uses the Regex.IsMatch method with RegexOptions.IgnoreCase.

RegexOptions.IgnoreCase. Sometimes text data has inconsistent casing.

Some data is uppercase, and some lowercase, but both are valid. The Regex type in the C# language by default is case-sensitive. RegexOptions.IgnoreCase relaxes this.



Example. The RegexOptions enum is typically passed as the last argument to a Regex method. This example shows how RegexOptions.IgnoreCase affects the result of the IsMatch method on an input that is in a different case.


Note: When IgnoreCase is specified, the match succeeds. Otherwise it fails. IgnoreCase will relax the regular expression.

C# program that uses RegexOptions.IgnoreCase constant

using System;
using System.Text.RegularExpressions;

class Program
    static void Main()
	// The input string has an uppercase trailing letter.
	const string value = "carroT";

	// Print result of IsMatch method:
	// ... With IgnoreCase;
	// ... And without any options set.
	Console.WriteLine(Regex.IsMatch(value, "carrot", RegexOptions.IgnoreCase));
	Console.WriteLine(Regex.IsMatch(value, "carrot"));



Other methods. You can use RegexOptions.IgnoreCase with other methods, not just IsMatch. Try it with Split, Matches and Match. It has the same effect when used with these methods.


Summary. The RegexOptions.IgnoreCase enumerated constant is simple in its intent and also its application. It will relax the requirements for an input with letters to be matched. Thus, the input string can have a capital or lowercase letter.


Tip: The RegexOptions.IgnoreCase argument is useful in many regular expressions.