using Regular exprestion to validate IP Address in Powershell?
I have this code in PowerShell and it does not work! any help?
I just need it to make sure that the string is a working IP not 999.999.999.999 or a normal string
just an IP [0....255].[0....255].[0....255].[0....255]
if (开发者_开发技巧$newIP -match "(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)") { $x = $True}
cheers
How about:
[bool]($newIP -as [ipaddress])
Here is a more compact one:
\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b
or even shorter
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
This works fine and errors if not full IP
$Name = "1.1" ; [bool]($Name -as [ipaddress] -and ($Name.ToCharArray() | ?{$_ -eq "."}).count -eq 3)
The following will not work ^(?:[0-9]{1,3}.){3}[0-9]{1,3}$ Take for example the part that needs to match the last octet from the IP Address [0-9]{1,3} - This will not match only number in the interval 0 to 255 Your best approach will be to dived checking of a single octet to 250 to 255; 240 to 249; 100 to 199; 10 to 99; and 0 to 99.
精彩评论