HOW TO MAKE A REGEX MATCH CASE INSENSITIVE?

Simons Hood

Total Post:70

Points:494
Posted by  Simons Hood
C# 
C#
 1164  View(s)
Ratings:
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.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to make a regex match case insensitive?

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!