Home > DeveloperSection > Forums > How to make a regex match case insensitive?
Simons Hood
Simons Hood

Total Post:70

Posted on    February-03-2014 12:48 AM

 C# C# 

 1 Reply(s)
 1138  View(s)
Rate this:

I have following regular expression for postal code of Canada.

^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$

It is working fine but accepts only Capital letters. I want it work for both capital and small letters.

Pravesh Singh

Total Post:412

Posted on    February-03-2014 1:03 AM

Hi Samuel,

Just use the option IgnoreCase,

So your regex creation could look like this

Regex r = new Regex(@"^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$", RegexOptions.IgnoreCase);

I removed also all your {1} because it is superfluous. Every item is per default matched once, no need to state this explicitly.

Don't want to miss updates? Please click the below button!

Follow MindStick