[Detail] Java determines whether a string is a legal ip

Judging whether the string is a legal IP is almost a must-test for the school recruitment interview every year

Next, I will use Java code to analyze this test site

/**
 * FILENAME : Judge
 * Author : HangLi
 * Data : 2020/10/27 9:03
 * Description : judge ip
 **/
public class Judge {

    public static void main(String[] args){
        // 定义正则表达式
        String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
        String str1 = "aaa@";
        String str2 = "aaaaaaa";
        String str3 = "[email protected]";

        // 判断字符串变量是否与正则表达式匹配
        if (str1.matches(regex)){
            System.out.println(str1+"是一个合法的E-mail地址");
        }
        if (str2.matches(regex)){
            System.out.println(str2+"是一个合法的E-mail地址");
        }
        if (str3.matches(regex)){
            System.out.println(str3+"是一个合法的E-mail地址");
        }
    }

}

The most important part of this part is to examine the understanding of regular expressions 

Guess you like

Origin blog.csdn.net/m0_37218227/article/details/109303875