I have verified that the regex the (slightly modified) Perl code produces, and the regex my PowerShell code produces are in fact equal - and I see no features used that should make it behave differently (to my knowledge; there are a few differences in the engines, but none of them should come into play here).
The author of the Perl code, Salvador Fandino, claims that "This module exports the $IPv6_re regular expression that matches any valid IPv6 address as described in "RFC 2373 - 2.2 Text Representation of Addresses but "::". Any string not compliant with such RFC will be rejected."Since validating IPv4 addresses is part of validating an IPv6 address, you can find a regex for that in the code as well, but I have a more thorough article on validating or extracting IPv4 addresses from text here.
function Test-IsValidIPv6Address { param( [Parameter( Mandatory = $True, HelpMessage = 'Enter IPv6 address to verify' ValueFromPipeline = $True )] [string] $IP) $IPv4Regex = '(((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))' $G = '[A-Fa-f\d]{1,4}' # In a case insensitive regex, such as they by default are in PowerShell, you can use: #$G = '[a-f\d]{1,4}' $Tail = @(":", "(:($G)?|$IPv4Regex)", ":($IPv4Regex|$G(:$G)?|)", "(:$IPv4Regex|:$G(:$IPv4Regex|(:$G){0,2})|:)", "((:$G){0,2}(:$IPv4Regex|(:$G){1,2})|:)", "((:$G){0,3}(:$IPv4Regex|(:$G){1,2})|:)", "((:$G){0,4}(:$IPv4Regex|(:$G){1,2})|:)") [string] $IPv6RegexString = $G $Tail | foreach { $IPv6RegexString = "${G}:($IPv6RegexString|$_)" } $IPv6RegexString = ":(:$G){0,5}((:$G){1,2}|:$IPv4Regex)|$IPv6RegexString" $IPv6RegexString = $IPv6RegexString -replace '\(' , '(?:' # make all groups non-capturing [regex] $IPv6Regex = $IPv6RegexString if ($IP -imatch "^$IPv6Regex$") { $true } else { $false } }
PS C:\> Test-IsValidIPv6Address -IP fe80::a584:1246:389b:fff True PS C:\> Test-IsValidIPv6Address -IP fe80::a584:1246:389b: False
Also see my main PowerShell regular expressions article.
:(?::[a-f\d]{1,4}){0,5}(?:(?::[a-f\d]{1,4}){1,2}|:(?:(?:(?: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-1]?[0-9]{1,2}))|(?::[a-f\d]{1,4}){1,2})|:))Powershell
Minimum cookies is the standard setting. This website uses Google Analytics and Google Ads, and these products may set cookies. By continuing to use this website, you accept this.