Home
C#
RegexOptions.IgnoreCase Example
Updated Sep 23, 2023
Dot Net Perls
RegexOptions.IgnoreCase. Sometimes text data has inconsistent casing. Some data is uppercase, and some lowercase, but both are valid.
Enum info. The Regex type in the C# language by default is case-sensitive. But RegexOptions.IgnoreCase, an enum value, relaxes this.
Regex.Match
Example. The RegexOptions enum is a Regex argument. 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.
using System; using System.Text.RegularExpressions; // 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"));
True False
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.
Regex.Split
Regex.Matches
A summary. RegexOptions.IgnoreCase will relax the requirements for an input with letters to be matched. Thus, the input string can have a capital or lowercase letter.
A final note. The RegexOptions.IgnoreCase argument is useful in many regular expressions. Before using ToLower(), consider using IgnoreCase.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 23, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen