Java uses regular expressions to verify IP V4, IP V6

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.