Java Regex Match IP address

Source: Internet
Author: User

Reference:

[1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/

Import Java.util.regex.matcher;import Java.util.regex.pattern;public class ipaddressvalidator{    private Pattern pattern;    Private Matcher Matcher;    private static final String Ipaddress_pattern = "^ ([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \. "+" ([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \. "+" ([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \. "+" ([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) $ ";    Public Ipaddressvalidator () {  pattern = pattern.compile (Ipaddress_pattern);    }   /**    * Validate IP address with regular expression    * @param IP IP address for validation    * @return true valid I P address, false invalid IP address    *    /public Boolean validate (final String IP) {  matcher = Pattern.matcher (IP);  return matcher.matches ();    }}

Description

^ #start of the line (#  start of group #1   [01]?\\d\\d? #    Can be one or both digits. If three digits appear, it must start either 0 or    # e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9])    |#    ... or   2[0- 4]\\d#    start with 2, follow by 0-4 and end with any digit (2[0-4][0-9])    |           #    ... or   25[0-5]      #    start with 2, follow by 5 and ends with 0-5 (25[0-5])) # End of  group #2  \ .            #  follow by a dot "." ....            # Repeat with 3 times (3x) $ #end of the line

Whole combination means, digit from 0 to 255 and follow by a dot ".", repeat 4 time and ending with no dot "." Valid IP address format is "0-255.0-255.0-255.0-255"

Java Regex Match IP address

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.