tags:

views:

203

answers:

1

I'm using System.Text.RegularExpressions.Regex.IsMatch(testString, regexPattern) to do some searches in strings.

Is there a way to specify in the regexPattern string that the pattern should ignore case? (I.e. without using Regex.IsMatch(testString, regexPattern, RegexOptions.IgnoreCase))

+5  A: 

(?i) within the pattern begins case-insensitive matching, (?-i) ends it. That is,

(?i)foo(?-i)bar

matches FOObar but not fooBAR.

EDIT: I should have said (?-i) begins case-sensitive matching - if you just want the whole pattern to be case-insensitive then you don't need to "end" the (?i).

stevemegson
+1 I didn't know about this! Nicely done!
Andrew Hare
Is the ending (?-i) required or can it be omitted?
Chris
It can be omitted without error.
Andrew Hare