How to make a regex match case insensitive?

Total Post:70


 1670  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.

  1. 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


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.