Phone Numbers:
- simple match that allows dashes as separators:
^\d{3}-\d{3}-\d{4}$ - allows periods, dashes or spaces as separators:
^(\(\d{3}\)|\d{3})[\s\.-]?\d{3}[\s\.-]?\d{4}$ - Matches to to the North American Numbering Plan (2009):
^(\([2-9][0-8]\d\)|[2-9][0-8]\d)[\s\.-]?[2-9]\d{2}[\s\.-]?\d{4}$
Postal Codes:
- simple match:
^([A-Z]\d[A-Z])\ {0,1}(\d[A-Z]\d)$ - allows unused “W” and “Z” as first characters:
^([ABCEGHJKLMNPRSTVWXYZ]\d[ABCEGHJKLMNPRSTVWXYZ])\ (\d[ABCEGHJKLMNPRSTVWXYZ]\d)$ - 2009 rules:
^[A-CEGHJ-NPR-TVXY]\d[A-CEGHJ-NPR-TV-Z][- /]?\d[A-CEGHJ-NPR-TV-Z]\d$
U.S. Zip Codes:
- Allows 5 and 5+4 zip codes:
^[0-9]{5}([- /]?[0-9]{4})?$
Roman Numerals:
- Unlike the ones I found all over the internet, this one actually works (up to 3999):
^(M{0,3})((D?C{0,3})|C[DM])((L?X{0,3})|X[LC])((V?I{0,3})|I[VX])$
Reading CSV:
- honours quoted strings:
^(?:([^",]+))?(?=,)|(?<=,)(?:[^",]*)?(?=[,$])|((?<=")[^"(\s*,)][^"]*(?="))|(?<=")(?=")|(?<=,)(?:[^,])*(?=$) - Also see Regex for parsing CSV data
C# Numeric Types:
- sbyte: (integer -128 to 127)
(\+|(?<neg>\-))?(?<![0-9\.,])0*((1[0-1][0-9]?)|(12(?(neg)[0-8]|[0-7]))|([1-9][0-9]?))(?!([\s,]\d{3})|(\.?\d)) - byte: (integer 0 to 255)
\+?(?<![\d\.\-,])0*((1?[\d]{0,2})|(2(([\d]?)|([0-4][\d])|(5[0-5]))))(?!([\s,]\d{3})|(\.?\d)) - short: (integer -32768 to 32767)
(\+|(?<neg>\-))?(?<![0-9\.,])0*(([1-2](\d?[\s,]?\d{3}))|(3(([0-1][\s,]?\d{3})|(2[\s,]?(([0-6]\d{2})|(7(([0-5]\d)|(6(?(neg)[0-8]|[0-7]))))))))|(\d[\s,]?)?[\d]{1,3})(?!([\s,]\d{3})|(.\d)|\d) - ushort: (integer 0 to 65535)
\+?(?<![0-9\.,\-])0*(([1-5](\d?[\s,]?\d{3}))|(6(([0-4][\s,]?\d{3})|(5[\s,]?(([0-4]\d{2})|(5(([0-2]\d)|(3[0-5])))))))|(\d[\s,]?)?[\d]{1,3})(?!([\s,]\d{3})|(.\d)|\d) - int: (integer -2,147,483,648 to 2,147,483,647)
(\+|(?<neg>\-))?(?<![0-9\.,])0*((\d{1,3}((?<s>([\s,]?))\d{3}((\k<s>)\d{3})?)?)|(1(((?<s>([\s,]?))\d{3}((\k<s>)\d{3}){2})|(\d{0,6})))|(2(?<s>([\s,]?))(((0\d{2})|(1[0-3]\d)|(14[0-6]))((\k<s>)\d{3}){2}|(147(\k<s>)(((([0-3]\d{2})|(4[0-7]\d)|(48[0-2]))(\k<s>)\d{3})|(483(\k<s>)(([0-5]\d{2})|(6[0-3]\d)|(64(?(neg)[0-8]|[0-7])))))))))(?!(\k<s>\d{3})|(.\d)|\d) - uint: (integer 0 to 4,294,967,295)
\+?(?<![0-9\.,\-])0*((\d{1,3}((?<s>([\s,]?))\d{3}((\k<s>)\d{3})?)?)|([0-3](((?<s>([\s,]?))\d{3}((\k<s>)\d{3}){2})|(\d{0,6})))|(4(?<s>([\s,]?))((([0-1]\d{2})|(2[0-8]\d)|(29[0-3]))((\k<s>)\d{3}){2}|(294(\k<s>)(((([0-8]\d{2})|(9[0-5]\d)|(96[0-6]))(\k<s>)\d{3})|(967(\k<s>)(([0-1]\d{2})|(2[0-8]\d)|(29[0-5]))))))))(?!(\k<s>\d{3})|(.\d)|\d) - long: (integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
(\+|(?<neg>\-))?(?<![0-9\.,])0*((\d{1,3}((?<s>([\s,]?))\d{3}((\k<s>)\d{3}){0,4})?)|([0-8](((?<s>([\s,]?))\d{3}((\k<s>)\d{3}){5})|(\d{0,18})))|(9(?<s>([\s,]?))((([0-1]\d{2})|(2[0-1]\d)|(22[0-2]))((\k<s>)\d{3}){5}|(223(\k<s>)(((([0-2]\d{2})|(3[0-6]\d)|(37[0-1]))((\k<s>)\d{3}){4})|(372(\k<s>)0(((([0-2]\d)|(3[0-5]))((\k<s>)\d{3}){3})|(36(\k<s>)(((([0-7]\d{2})|(8[0-4]\d)|(85[0-3]))((\k<s>)\d{3}){2})|(854(\k<s>)(((([0-6]\d{2})|(7[0-6]\d)|(77[0-4]))(\k<s>)\d{3})|(775(\k<s>)(([0-7]\d{2})|(80(?(neg)[0-8]|[0-7])))))))))))))))(?!(\k<s>\d{3})|(.\d)|\d) - ulong: (integer 0 to 18,446,744,073,709,551,615)
\+?(?<![\d\.\-,])0*((\d{1,3}((?<s>([\s,]?))\d{3}((\k<s>)\d{3}){0,4})?)|((([0-9])|(1[0-7]))(((?<s>([\s,]?))\d{3}((\k<s>)\d{3}){5})|(\d{0,18})))|(18(?<s>([\s,]?))((([0-3]\d{2})|(4[0-3]\d)|(44[0-5]))((\k<s>)\d{3}){5}|(446(\k<s>)(((([0-6]\d{2})|(7[0-3]\d)|(74[0-3]))((\k<s>)\d{3}){4})|(744(\k<s>)0(((([0-6]\d)|(7[0-2]))((\k<s>)\d{3}){3})|(73(\k<s>)(((([0-6]\d{2})|(70[0-8]))((\k<s>)\d{3}){2})|(709(\k<s>)(((([0-4]\d{2})|(5[0-4]\d)|(550))(\k<s>)\d{3})|(551(\k<s>)(([0-5]\d{2})|(60\d)|(61[0-5]))))))))))))))(?!(\k<s>\d{3})|(.\d)|\d)