RegexOptions.IgnoreCase UsePass the RegexOptions.IgnoreCase argument to Regex functions for case-insensitive regular expression patterns.
This page was last reviewed on Dec 12, 2023.
RegexOptions.IgnoreCase. How can you match a pattern in a regular expression in a case-insensitive way? In .NET we can use the RegexOptions.IgnoreCase option.
Often, the strings we are processing may have inconsistent casing, with both lowercase and uppercase letters used. We only need one pattern to match these strings with IgnoreCase.
Example. In the Main function, we introduce a string with mixed lowercase and uppercase letters in. Then we try to match this string with patterns using Regex.IsMatch.
Part 1 We call Regex.IsMatch with an all-lowercase pattern. We use RegexOptions.IgnoreCase, and the mixed-case string is matched.
Part 2 Here we omit the RegexOptions.IgnoreCase option, and the IsMatch function returns False, as the cases are not the same.
Imports System.Text.RegularExpressions Module Module1 Sub Main() ' String has an uppercase letter. Dim value = "carroT" ' Part 1: use RegexOptions.IgnoreCase with a lowercase pattern. If Regex.IsMatch(value, "carrot", RegexOptions.IgnoreCase) ' This will be printed. Console.WriteLine("IS MATCH!") End If ' Part 2: omit IgnoreCase, so the lowercase pattern will no longer match. Console.WriteLine(Regex.IsMatch(value, "carrot")) End Sub End Module
Summary. In VB.NET programs, the RegexOptions.IgnoreCase enum is helpful on many Regex function calls. It helps make the regular expressions more flexible and useful.
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 Dec 12, 2023 (new).
© 2007-2024 Sam Allen.