December 2015
Intermediate to advanced
760 pages
14h 31m
English
In this recipe, we are going to explore some ways to use and test regular expressions.
Let's check out regular expressions in PowerShell:
$VerbosePreference = "Continue" #check if valid email address $str = "info@sqlbelle.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 ...Read now
Unlock full access