Using RegexOptions

The RegexOptions flags enum lets you tweak matching behavior.

A common use for RegexOptions is to perform a case-insensitive search:


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Regex.Match("a", "A", RegexOptions.IgnoreCase)); 
    }
}

The output:


a

Most of the RegexOptions flags can be activated within a regular expression itself, using a single-letter code as follows:


using System;
using System.Text.RegularExpressions;


class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Regex.Match("a", @"(?i)A"));

    }
}

The output:


a

You can turn options on and off throughout an expression as follows:


using System;
using System.Text.RegularExpressions;


class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Regex.Match("AAAa", @"(?i)a(?-i)a"));
    }
}

The output:


Aa

Regular expression options and Regular expressions code

RegexOptions Enum valueRegular expressions code
IgnoreCasei
Multilinem
ExplicitCapturen
Compiledc
Singlelines
IgnorePattern Whitespacex
RightToLeftr
ECMAScriptN/A
CultureInvariantN/A
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.