BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Regular Expressions
.NET 1.1+

Regular Expression Inline Options

The fourteenth part of the Regular Expressions in .NET tutorial looks at options that can be applied to a pattern using characters within a regular expression. There are five such options, which can be applied to all or part of a pattern.

Regular Expression Options

There are a number of options that you can apply to your regular expressions to modify the way in which patterns are matched. Options can either be specified inline, using characters within a regular expression, or using an enumeration that you pass to a parameter of one of the Regex class's methods. For example, in the article describing regular expression anchors, we used a parameter to specify that the matching should be applied to a multiline string. In this article we'll look at the inline options.

There are five options that you can include within a regular expression. Each is represented by a single letter. They are:

  • i. Specifies that matching should be case-insensitive. With this option enabled, upper case letters will be matched when searching for lower case items and vice versa.
  • m. Enables multiline matching. When enabled, the ^ and $ anchors match the start and end of a line. Without the option, they match the start and end of the entire string.
  • n. Specifies that groups should not be captured unless they are explicitly named.
  • s. This option enables single line matching. In this mode, the wildcard character (.) matches any character. When not enabled, the wildcard match any character except line breaks.
  • x. Applying this option causes white space in the regular expression pattern to be ignored unless it is escaped. This can make complex regular expressions easier to read.

Applying Inline Options

There are two ways in which you can apply an inline option. To demonstrate, first consider the following sample code. This looks for the text "Apple", preceded by zero or more word characters. As the regular expression is case-sensitive it matches "Apple" but not "Pineapple".

string input = "Apple, Pineapple, Orange";

foreach (Match match in Regex.Matches(input, @"\w*Apple"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match, match.Index);
}

/* OUTPUT
  
Matched 'Apple' at index 0
  
*/

The first way to apply an option is to place it at the position within the regular expression at which you wish it to start to take effect. Once applied, it is used until the end of the pattern is reached, or until it is disabled with another inline code.

To apply an option in this manner, prefix it with a question mark and surround both characters with parentheses. For example, "(?i)" would turn off case-sensitivity, as in the following example code:

string input = "Apple, Pineapple, Orange";

foreach (Match match in Regex.Matches(input, @"\w*(?i)Apple"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match, match.Index);
}

/* OUTPUT
  
Matched 'Apple' at index 0
Matched 'Pineapple' at index 7
            
*/

You can also apply inline options to a single group. To do so, prefix them with a question mark and separate them from the capturing characters in the group with a colon (:). The following sample applies the case-insensitive option to the group containing the pattern, "Apple":

string input = "Apple, Pineapple, Orange";

foreach (Match match in Regex.Matches(input, @"\w*(?i:Apple)"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match, match.Index);
}

/* OUTPUT
  
Matched 'Apple' at index 0
Matched 'Pineapple' at index 7
            
*/

Removing Inline Options

Often you will need to disable an option after enabling it. You can remove an option using the same syntax but with a minus sign (-) before the option's letter. The example below shows a pattern with the case-insensitive option first enabled, then switched off.

string input = "Apple, Pineapple, Orange";

foreach (Match match in Regex.Matches(input, @"(?i)\w*(?-i)Apple"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match, match.Index);
}

/* OUTPUT
  
Matched 'Apple' at index 0
            
*/

Combining Inline Options

Inline options can be applied in groups by specifying more than one letter at a time; it is not necessary to include each option individually. For example, the final code sample applies two options. The 'i' indicates case-insensitivity and the 'x' specifies that the space in the pattern should be ignored.

string input = "Apple, Pineapple, Orange";

foreach (Match match in Regex.Matches(input, @"(?ix)\w* Apple"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match, match.Index);
}

/* OUTPUT
  
Matched 'Apple' at index 0
Matched 'Pineapple' at index 7
            
*/

NB: You can enable and disable options at the same time. Options to enable should appear before any minus sign and those to disable should appear after one. For example, (?i-mx) will enable case-insensitivity and disable multiline mode and ignoring unescaped whitespace.

6 December 2015