Use Regular Expression to Validate Input

by Nov 17, 2009

PowerShell can use regular expressions to validate user input. All you need is a regular expression that adequately defines the pattern you are seeking. Fortunately, you do not need to create regular expressions from scratch. Simply Google them and then replace the pattern string in the example below to validate other user input:

$pattern = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)' +

do {
$ip = Read-Host "Enter IP"
} while ($ip -notmatch $pattern)

Twitter This Tip! ReTweet this Tip!