directory

Conventional judgment method

Regular expression judgment method


Hello, everyone, I’m grey ape!

The algorithm that determines whether a string is an IP address

To implement the algorithm, we should first know which conditions the IP address should be based on:

Judgment principle:

  1. Check whether the value is a string of 7 to 15 characters (0.0.0.0 to 255.255.255.255).
  2. Can you divide it into four decimal points
  3. Whether each paragraph is a number
  4. Whether each digit is between 0 and 255

Only when the above four conditions are met, is the IP format correct.

There are two simple methods to achieve this algorithm, one is conventional judgment, the other is to use regular expression to judge, the following two methods in the form of function, you can directly copy the function to use, pass in the parameter is to judge the string, return is a Boolean variable.

(This algorithm is implemented in Java language, please make appropriate adjustment when using!)

 

Conventional judgment method

// Check if the character is IP
public boolean isCorrectIp(String ipString) {
        // 255.255.255.255 = 0.0.0.0-255.255.255.255
	if (ipString.length()<7||ipString.length()>15) {
		return false;
	}
	//2, check whether the decimal point can be divided into four segments
	String[] ipArray = ipString.split("\ \.");		
	if(ipArray.length ! =4) {
		return false;
	}
	for (int i = 0; i < ipArray.length; i++) {
		//3, check whether each paragraph is a number
		try {
			int number = Integer.parseInt(ipArray[i]);
			//4. Check whether each digit is between 0 and 255
			if (number <0||number>255) {
        			return false; }}catch (Exception e) {
			return false; }}return true;
	}
Copy the code

 

Regular expression judgment method

// Use regular expressions to determine whether a character is an IP
public boolean isCorrectIp2(String ipString) {
	String ipRegex = "\ \ d {1, 3} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3} \ \ \ \ d {1, 3}";	// Regular expression for the IP address
	// If the first three values are satisfied, check whether each segment is between 0 and 255
	if (ipString.matches(ipRegex)) {
		String[] ipArray = ipString.split("\ \.");
		for (int i = 0; i < ipArray.length; i++) {
			int number = Integer.parseInt(ipArray[i]);
			//4. Check whether each digit is between 0 and 255
			if (number <0||number>255) {
				return false; }}return true;
	}
	else {
		return false;	// If the regular expression does not match, false is returned}}Copy the code

Test results:

In the program to the decimal point as the basis for segmentation, the use of “\.” Instead of using “. The reasons are as follows:

Call the split (“.” ) method, the program will put “. As pattern matching characters, as in regular expressions. It’s the same for any character. So here it is. Is a special character.

Call the split (” \.” Method error because “\b \t \n \f \r \* \” escape characters do not include” \.” , so the program will report an error!

Hence the need for “\.” To represent the “\.” String, thus separating characters by decimal point.

 

Think useful remember to like attention yo!

Big bad Wolf accompany you to progress together!