Testing regular expressions
In this recipe we are going to explore some ways to use and test regular expressions.
How to do it...
Let's check out regular expressions in PowerShell.
Open PowerShell ISE. Go to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Add the following script and run it:
$VerbosePreference = "Continue" #check if valid email address $str = "belle@sqlmusings.com" $pattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|ca|mil|biz|info|mobi|name|aero|jobs|museum)$" if ($str -match $pattern) { Write-Verbose "Valid Email Address" } else { Write-Verbose "Invalid Email Address" } #another way to test [Regex]::Match($str, $pattern) #can also use regex in switch $str = "V1A 2V1" $str = "90250" switch -regex ($str) { "(^\d{5}$)|(^\d{5}-\d{4}$)" { Write-Verbose "Valid US Postal Code" } "[A-Za-z]\d[A-Za-z]\s*\d[A-Za-z]\d" { Write-Verbose "Valid Canadian Postal Code" } default { Write-Verbose...