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 Options

The fifteenth part of the Regular Expressions in .NET tutorial continues to looks at options that can be applied to regular expressions when matching and substituting text. This article looks at the use of the RegexOptions enumeration.

RegexOptions

In the previous article in the tutorial we looked at the inline options that you can apply within a regular expression. The five available options allow you to modify the matching behaviour for an entire pattern, a group, or from any starting point within the string.

The same five options can be applied without using inline characters, by passing a value from the RegexOptions enumeration to the method that you are using to match text or perform substitutions. Using this approach applies the options to the entire pattern, except where inline options override the RegexOptions value.

In addition to the five options we saw earlier, there are several values that are only available using the enumeration. These allow you to apply options that are not permitted with inline characters. They provide features such as reversing the search order for the operation, ignoring the user's current culture and compiling regular expressions for improved performance.

Basic Options

The basic options work in much the same manner as they do when applied using inline characters. They are represented by the following constants in the RegexOptions enumeration:

  • None. The default configuration that is used when no options are provided.
  • IgnoreCase. This option works in the same manner as the 'i' inline option. It causes the case of individual characters in the input string to be ignored.
  • Multiline. This option specifies that the input string should be treated as multiline text, affecting the operation of the start and end of line anchors. It is the equivalent of the 'm' inline option.
  • Singleline. As with the 's' option, this constant indicates that the input string should be considered as a single line of text. This changes the behaviour of the start and end of line anchors.
  • ExplicitCapture. This option prevents the capturing of groups that are unnamed. Its behaviour is similar to the 'n' option.
  • IgnorePatternWhitespace. The equivalent of the inline 'x' option, this value indicates that white space in the regular expression should be ignored unless it is escaped.

NB: The RegexOptions enumeration is defined as a bit field using the Flags attribute, so you can combine multiple options using bitwise logical operators.

Applying Options

To apply the above options you pass a RegexOptions value to the options parameter of the regular expression engine method that you wish to use. The code below demonstrates this with the Match method. The first call to Match does not provide any options, so the method uses the RegexOptions.Default value. The second call specifies that the operation should ignore the character casing, so more words are matched.

string input = "Banana, banana, BANANA!";

Console.WriteLine("Default Matching");
foreach (Match match in Regex.Matches(input, "banana"))
{
    Console.WriteLine("Matched '{0}' at index {1}", match.Value, match.Index);
}

Console.WriteLine("\nCase-Insensitive Matching");
foreach (Match match in Regex.Matches(input, "banana", RegexOptions.IgnoreCase))
{
    Console.WriteLine("Matched '{0}' at index {1}", match.Value, match.Index);
}


/* OUTPUT

Default Matching
Matched 'banana' at index 8

Case-Insensitive Matching
Matched 'Banana' at index 0
Matched 'banana' at index 8
Matched 'BANANA' at index 16

*/

You can use the options in a similar manner with the Replace method. For example, the code below uses case-sensitive and case-insensitive matching during a substitution operation. The second call, which ignores the case, surrounds all three words with brackets.

string input = "Banana, banana, BANANA!";

Console.WriteLine("Default Substitution");
Console.WriteLine(Regex.Replace(input, "banana", "[$0]"));

Console.WriteLine("\nCase-Insensitive Substitution");
Console.WriteLine(Regex.Replace(input, "banana", "[$0]", RegexOptions.IgnoreCase));


/* OUTPUT

Default Substitution
Banana, [banana], BANANA!

Case-Insensitive Substitution
[Banana], [banana], [BANANA]!

*/
18 December 2015