Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (?i) in a .NET regular expression mean?

Tags:

c#

.net

regex

In our code there is a regular expression of the following form:

string regex = @"(?i)foo=(BAR?-[A-Z]+(33|34)?)";

What does the "(?i)" at the beginning of the regex match/do? I've looked through the .NET regex documentation and can't seem to figure out what (?i) would mean. Thanks!

like image 755
Polaris878 Avatar asked Jul 12 '11 15:07

Polaris878


2 Answers

(?i) activates case-insensitive matching.

Reference: MSDN, Regular Expression Options (highlighting by me):

You can specify options for regular expressions in one of three ways:

  • In the options parameter of a System.Text.RegularExpressions.Regex class constructor or static (Shared in Visual Basic) pattern-matching method, such as Regex.Regex(String, RegexOptions) or Regex.Match(String, String, RegexOptions). [...]

  • By applying inline options in a regular expression pattern with the syntax (?imnsx-imnsx). The option applies to the pattern from the point that the option is defined to either the end of the pattern or to the point at which the option is undefined by another inline option. [...]

  • By applying inline options in a particular grouping construct in a regular expression pattern with the syntax (?imnsx-imnsx:subexpression). [...]

like image 56
Heinzi Avatar answered Sep 24 '22 04:09

Heinzi


(?i) means: Ignore case option enabled. It's equivalent to call Regex.Matches with 3rd param RegexOptions.IgnoreCase

like image 42
Kirill Polishchuk Avatar answered Sep 25 '22 04:09

Kirill Polishchuk