How to make a regex match case insensitive?

Total Post:70

Points:494
C# 
C#
 1472  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. 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.

      Modified On Apr-04-2018 04:02:30 AM

Answer

NEWSLETTER

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