Java Regex IP Address Example

Chapter: Regular Expression Last Updated: 21-07-2016 18:39:26 UTC

Program:

            /* ............... START ............... */
                
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaRegexIPAddress {
	public static boolean isValidIP(String ipAddress) {

		Pattern ptn = Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$");
		Matcher matcher = ptn.matcher(ipAddress);
		return matcher.find();
	}

	public static void main(String a[]) {
		System.out.println("10.18.55.20 is valid? " + JavaRegexIPAddress.isValidIP("10.18.55.20"));
		System.out.println("121.2a.45.89 is valid? " + JavaRegexIPAddress.isValidIP("121.2a.45.89"));
		System.out.println("9.23.45 is valid? " + JavaRegexIPAddress.isValidIP("9.23.45"));
	}

}

                /* ............... END ............... */
        

Output

10.18.55.20 is valid? true
121.2a.45.89 is valid? false
9.23.45 is valid? false

Notes:

  • Java Regex IP Address used to validate IP address using regular expression.

Tags

Regex IP Address, Java

Similar Programs Chapter Last Updated
Java Regular Expression Match Single Digit Regular Expression 15-12-2016
Java Regular Expression To Check Number With Dot Separator And Two Decimal Regular Expression 14-12-2016
Java Regular Expression To Check Numeric Regular Expression 14-12-2016
Java Regular Expression Digit Only Regular Expression 14-12-2016
Java Check Date Format Example Regular Expression 14-09-2016
Java Validate email Address Regular Expression 13-09-2016
Java Replace All Occourances Of String Example Regular Expression 13-09-2016
Java Make First Letter Of Each Word In Uppercase Regular Expression 13-09-2016
Java Match Phone Numbers Using Regular Expression Regular Expression 11-09-2016
Java Last Index Of Word Using Regular Expression Regular Expression 11-09-2016
Java Find Word Occurrence Using Regular Expression Regular Expression 11-09-2016
Java Splitting String Using Regular Expression Regular Expression 11-09-2016
Java Regex Pattern Split Example Regular Expression 21-06-2016
IP Address From String In Java Regex Regular Expression 21-07-2016
Validate UserName Using Regular Expression In Java Regular Expression 22-09-2018
Regular Expression In Java Example Regular Expression 09-07-2016
Pattern Matching In Java Example Regular Expression 09-07-2016

1