简体   繁体   中英

Regular Expression for Canadian Postal Code to also allow for lowercase letters or a dash after the first three characters

^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$

I would like to add lower case characters to this regex (currently only capital letters are allowed). I would also like the separator in the middle: '[ ]?' to also allow for a dash (currently allows a space or no space). How do I ammend the above regex to account for these possible variations. Language being used is .net.

When you use the regular expression constructor, pass the RegexOptions.IgnoreCase flag .

For the second requirement, change [ ]? to [ -]? .

Summary:

var regex = new Regex(@"^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ -]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$", RegexOptions.IgnoreCase);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM