Posts Tagged ‘regex’

Validation Expressions – Postal Codes – US and Canada

Here’s the regular expression for US & Canada Postal Codes:
^(\d{5}(( |-)\d{4})?)|([A-Za-z]\d[A-Za-z]( |-)\d[A-Za-z]\d)$

Stipping Invalid Characters from a String

First, it’s called “stripping!” Second, to strip these characters from a string, you must use the Regex.Replace method. — as denoted by microsoft here.

using System.Text.RegularExpressions

Regex.Replace(strInput, @”[^\w\.@-]”, “”);

This will strinp all invalid characters EXCEPT the period, @, and hyphen.