正则匹配规则及常用模式串(待续)

正则匹配:

1.非打印字符:
\f 换页符; \n 换行符; \r 回车符 ; \t制表符; \v垂直制表符; \s任何空白字符

2.特殊字符:
^ 要求模式串中^后的字符必须在匹配串的开头,如abcde和 ^abc匹配
$ 要求模式串中$前的字符必须在匹配串的结尾,如abcde和 cde$匹配
* 要求前面的字符出现0次或多次
+ 要求前面的字符出现1次或多次
? 要求前面的字符出现0次或1次
() 表示将()中的内容视为一个整体,如^(abc)表示匹配串必须以abc开头
[] 表示取[]中内容的其中之一,如[abc]表示a或b或c
{} 修饰匹配次数,用于限定符,如a{2,5}表示出现2-5次a
| 表示或者的意思,如a|b可以匹配a或b
. 可以匹配任何换行符\n之外的任何字符

3.限定符(6种): 表示出现次数
* + ? {n} {n,} {n,m}

4.元字符
[^abc] 表示对abc取反,不匹配abc中的任何一个字符
[^a-z] 表示对a-z取反,不匹配a-z中的任何一个字符
\b 匹配一个单词边界,如bc\b可以和abc匹配,但不和abcd匹配
\B 不匹配一个单词边界,如bc\B可以和abcd匹配,但不和abc匹配
\d 匹配一个数字字符。等价于 [0-9]
\D 匹配一个非数字字符。等价于 [^0-9]
\w 匹配字母、数字、下划线。等价于’[A-Za-z0-9_]’
\W 匹配非字母、数字、下划线。等价于 ‘[^A-Za-z0-9_]’。

Java中使用正则匹配:

	public static boolean match(String patternString, String matcherString){
		Pattern pattern = Pattern.compile(patternString);
		Matcher matcher = pattern.matcher(matcherString);
		return matcher.matches();
	}

常用的模式串或方法:

IP

^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.
((1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.){2}
(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$

子网掩码

public class MaskUitl{
	//先检查是否是一个IP,再检查是否符合掩码规范,最后检查该掩码是否和给定IP匹配
	public static boolean checkMask(String ip,String mask) {
		if (MaskUitl.isIPAddressHost(mask) && MaskUitl.isSubnetMaskHost(mask)) {
      		return MaskUitl.isLegalSubnetMask(ip, mask)) 
		}
	}
	//检查掩码基本格式
    public static boolean isSubnetMaskHost(String ipStr) {
        if (ipStr.length() == 0) {
            return false;
        }
        int maskInt = IPtoInt(ipStr);
        maskInt = ~maskInt + 1;
        return  ((maskInt & (maskInt - 1)) == 0);
    }
    private static int IPtoInt(String ipStr) {
        if (ipStr == null || ipStr.length() == 0) return 0;
        int ipInt = 0;
        String[] ipArr = ipStr.split("\\.");
        for (int i = 0; i < 4; i++) {
            if (Integer.parseInt(ipArr[i]) > 255) return 0;
            ipInt |= (Integer.parseInt(ipArr[i]) & 0xFF) << (24 - i * 8);
        }
        return ipInt;
    }
    //检查是否符合IP基本格式
    public static boolean validateIPAddress(String ipStr) {
        if (ipStr == null || ipStr.length() == 0) {
            return false;
        }
        String pattern_str = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.\n" +
                "((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.){2}\n" +
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        Pattern pattern = Pattern.compile(pattern_str);
        Matcher matcher = pattern.matcher(ipStr);
        return matcher.matches();
    }
   	//检查掩码和相对应的IP是否匹配
    public static boolean isLegalSubnetMask(String ip, String subnetMask) {
        try {
            if (ip.length() == 0 || !validateIPAddress(ip) || subnetMask.length() == 0)
                return false;
            int ip_int = IPtoInt(ip);
            int subnetMask_int = IPtoInt(subnetMask);
            int tmp = ip_int & (~subnetMask_int);
            if (tmp == 0) {
                return false; // not Net Service IDentity
            } else if (tmp == (~subnetMask_int)) {
                return false; // not broadcast
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

中文字符:[\u4e00-\u9fa5]

猜你喜欢

转载自blog.csdn.net/weixin_43724742/article/details/84203625