regex – C# regexoptions.ignorecase doesn’t seem to work – Education Career Blog

Using the following code:

string pat = @"ENGL101_.*_(.*)";
Regex r = new Regex(pat, RegexOptions.IgnoreCase);

Matches:
ENGL101_BELIEVE_WRIGHTSTONE.docx

but not:
Engl101_ThisIBelieve_Williams.docx

IgnoreCase is on– what’s the issue??

,

I can’t replicate this problem; both the strings appear to match the expression.

STAThread
static void Main()
{
    string pat = @"ENGL101_.*_(.*)";
    Regex r = new Regex(pat, RegexOptions.IgnoreCase);

    Console.WriteLine(r.IsMatch(@"ENGL101_BELIEVE_WRIGHTSTONE.docx"));
    Console.WriteLine(r.IsMatch(@"Engl101_ThisIBelieve_Williams.docx"));
}

Output:

True
True

The problem must be something else, perhaps?

,

Can’t repro – tried in Snippet Compiler and:

    public static void RunSnippet()
    {
        string pat = @"ENGL101_.*_(.*)";
        Regex r = new Regex(pat, RegexOptions.IgnoreCase);

        Match m = r.Match("ENGL101_BELIEVE_WRIGHTSTONE.docx");

        WL(m.Success);

        m = r.Match("Engl101_ThisIBelieve_Williams.docx");

        WL(m.Success);
    }

Returns

True  
True

,

I know this may sound obvious, but have you tried matching against

ENGl101_THISIBELIEVE_WILLIAMOS.docx

without ignore case?

Leave a Comment