Home
C#
RegexOptions.IgnoreCase Example
This page was last reviewed on 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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 23, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.