Packagecn.OutOfMemory.Snippets.Core;ImportJava.Util.Regex.Pattern;/** * A Collection of utilities relating to inetaddresses. */Public Class Inetaddressutils { Public Static voidMain(String[]Args){ StringAddr="192.168.1.2"; System.Out.println(Isipv4address(Addr)); } Private Inetaddressutils() { } Private Static Final PatternIpv4_pattern= Pattern.Compile( "^ (25[0-5]|2[0-4]\\d| [0-1]?\\d?\\d) (\ \. ( 25[0-5]|2[0-4]\\d| [0-1]?\\d?\\d)] {3}$ "); Private Static Final PatternIpv6_std_pattern= Pattern.Compile( "^ (?: [0-9a-fa-f]{1,4}:) {7}[0-9a-fa-f]{1,4}$"); Private Static Final PatternIpv6_hex_compressed_pattern= Pattern.Compile( ^ ((?: [0-9a-fa-f]{1,4} (?:: [0-9a-fa-f]{1,4}) *)::((?: [0-9a-fa-f]{1,4} (?:: [0-9a-fa-f]{1,4}) *)?) $"); Public Static BooleanIsipv4address(Final StringInput) { ReturnIpv4_pattern.Matcher(Input).Matches(); } Public Static BooleanIsipv6stdaddress(Final StringInput) { ReturnIpv6_std_pattern.Matcher(Input).Matches(); } Public Static BooleanIsipv6hexcompressedaddress(Final StringInput) { ReturnIpv6_hex_compressed_pattern.Matcher(Input).Matches (); } public boolean Isipv6address ( final string Input) Span class= "PLN" > { return (input| | Isipv6hexcompressedaddress (input }}
This article transferred from: http://outofmemory.cn/code-snippet/1197/java-usage-regular-expression-yanzheng-IP-v4-IP-v6-address
Java uses regular expressions to verify IP V4, IP V6