欢迎光临散文网 会员登陆 & 注册

java正则表达式的使用

2022-08-09 09:26 作者:虚云幻仙  | 我要投稿

/**
* 测试正则表达式
*/

public class RegularExpression1 {
   public static void main(String[] args) {
       String re = "[a]";
       //正则表达式使用字符串书写,用[]限定匹配的内容,用{}限定长度,当只有[]没有{}时限定长度为1
       String content = "a";
       System.out.println(content.matches(re));
       //对要操作的字符串调用.matches(String regex)方法,将正则表达式传入,返回boolean该字符串是否符合正则表达式,结果为true
       content = "aa";
       System.out.println(content.matches(re));
       //因为re限定字符串长度为1,所以结果为false
       re = "[a-z]";
       //[a-z]匹配所有小写字母
       content = "ab";
       System.out.println(content.matches(re));
       //结果为false,因为限定了字符串长度为1
       re = "[a-zA-Z0-9]";
       //需要匹配的字符都写进[]内并且不用分隔
       content = "2";
       System.out.println(content.matches(re));
       //结果为true
       re = "[123457-9]{3}";
       //匹配数字12345789,长度必须为3
       System.out.println(content.matches(re)+"false");
       content = "188";
       System.out.println(content.matches(re)+"true");
       re = "[0-9]{2,3}";
       //{2,3}限定长度最小为2最大为3,这里包头包尾
       System.out.println(content.matches(re)+"true");
       re = "[0-9]{2,}";
       //{2,}第二个数空着表示不限最大长度
       content = "434134132";
       System.out.println(content.matches(re)+"true");
       re = "[0-9]{0,}";
       //最小长度不能空着,{0,表示允许匹配内容长度为0
       content = "";
       System.out.println(content.matches(re)+"true");
       re = "123456a-z{5}";
       //不加[]时{5}只会匹配最后一个字符z,z前面的视为固定组合
       content = "123456a-zzzzz";
       System.out.println(content.matches(re));
       //123456a-和content的前半部分相匹配,z{5}和后面匹配,-和z也必须连着,结果为true
       //也就是说[]内无论写多少内容都是规定单个字符的取值范围

       re = "[^abcde12345]";
       //[^为取反,限定匹配字符不能是[]内的内容
       content = "5";
       System.out.println(content.matches(re)+"false");
       re = "[0-9]?";
       //长度限定符号?为限定长度{0,1}
       System.out.println(content.matches(re)+"true");
       content = "a";
       System.out.println(content.matches(re));
       //虽然长度为1符合{0,1}但内容a不满足0-9,结果为false
       re = "[0-9]*";
       //长度限定符号*为限定长度{0,}
       content = "";
       System.out.println(content.matches(re)+"true");
       re = "[0-9]+";
       //长度限定符号+为限定长度{1,}
       System.out.println(content.matches(re)+"false");
       re = "\\d";
       //预定义字符\d表示匹配数字[0-9] java字符串内\为转义字符,所以需要用\\来表示\,\d就变为了\\d
       content = "3";
       System.out.println(content.matches(re)+"true");
       re = "\\D";
       //大写为匹配除了[0-9]以外的字符
       System.out.println(content.matches(re)+"false");
       re = "\\n";
       //预定义字符\n为匹配换行符,这里同样需要两个\来表示\,如果写成\n会判定为字符串的换行符,\\n才是正则的预定义字符
       content = "\n";
       System.out.println(content.matches(re)+"true");
       re = "\\r";
       //预定义字符\r匹配回车符
       content = "\r";
       System.out.println(content.matches(re));
       re = "\\s";
       //预定义字符\s表示匹配任何空白字符,包括空格\s 制表符\t 换行符\n 回车\r等
       System.out.println(content.matches(re)+"true");
       content = "\n";
       System.out.println(content.matches(re)+"true");
       re = "\\S?";
       //大写表示匹配除了空白字符以外的任何字符,包括中文
       content = "空";
       System.out.println(content.matches(re)+"true");
       re = "\\t+";
       //预定义字符\t匹配制表符
       content = "\t";
       System.out.println(content.matches(re)+"true");
       re = "\\w*";
       //预定义字符\w表示匹配字母、数字和下划线{_a-zA-Z0-9}
       content = "_";
       System.out.println(content.matches(re)+"true");
       re = "\\W";
       //大写为匹配除了\w以外的字符
       System.out.println(content.matches(re)+"false");
       //String类的方法.split()也是使用了正则表达式,所以当遇到需要分割+*?时需要加\\来表示,直接写+*?为判定为长度限定符号,\+会判定为转义字符,或者也可以写成[+],包在[]内时会表示原意
       System.out.println(Arrays.toString("a+b*c?d".split("\\?")));
       //结果为[a+b*c, d]
       re = "[\\w空白字符]";
       //将预定义字符写在[]内会和括号内的其他内容一起组成限定范围
       content = "空";
       System.out.println(content.matches(re)+"true");
       re = "[\\w&&[^a-z]]";
       //在[]内添加&&[]会将&&前面内容与&&[]中的内容取交集,这里前面集合为{a-zA-Z0-9_}后面集合为非a-z,取交集为{A-Z0-9_}
       content = "a";
       System.out.println(content.matches(re)+"false");
       re = "[a-z]{1,2}\\d{4,5}";
       //组合定义,以字母a-z开头并且字母长度为1到2,后续接数字长度为4-5
       content = "a1234";
       System.out.println(content.matches(re)+"true");
       content = "ab54321";
       System.out.println(content.matches(re)+"true");
       //这时单一的{}不再代表总长度,每一组内容限定[]的前后顺序也必须相符,这里总长度为5-7,但具体哪一位匹配什么还是要看表达式
       re = "\\d?\\W*";
       //开头以1个或0个数字,后续限制内容为\\W长度不限
       content = "{:>?<";
       System.out.println(content.matches(re)+"true");
       re = "\\d[a-z]{2}";
       //\\d不加{}表示1位,后续两位a-z
       content = "2ed";
       System.out.println(content.matches(re)+"true");
       re = "(\\d{3,4})-(\\d{7,8})";
       //正则表达式中()同运算符(),括起来的部分优先处理
       content = "010-53456166";
       System.out.println(content.matches(re)+"true");
       re = "()\\d{3,4}-\\d{7,8}";
       System.out.println(content.matches(re)+"true");
       re = "\\(\\d{3,4}\\)-\\d{7,8}";
       //想在正则中表示(括号同样需要\\(
       System.out.println(content.matches(re)+"false");
       re = ".*";
       //预定义字符.代表匹配除了\n\r以外的任意字符
       System.out.println(content.matches(re)+"true");
       content = "\n";
       System.out.println(content.matches(re)+"false");
       re = "\\.*";
       //想表示.同样需要\\.或者[.]在方括号内不加\也可以
       content = "....";
       System.out.println(content.matches(re)+"true");
       re = "[a-d]|[x-z]";
       //或运算| 内容为前面集合的范围后者后面集合的范围
       content = "x";
       System.out.println(content.matches(re)+"true");
       re = "([a-d]|[x-z]){5}";
       //将前面用()括起来表示匹配a-d或x-z的字符,后面加{5}表示匹配5个该字符,这里每个字符单独判定,允许a-d和x-z混合的情况
       content = "abxdy";
       System.out.println(content.matches(re)+"true");
       re = "[a-d]|[x-z]{5}";
       //如果没有括起来,{5}的作用范围只影响[x-z],所以匹配范围为一个a-d或者五个x-z
       System.out.println(content.matches(re)+"false");
       content = "c";
       System.out.println(content.matches(re)+"true");
       re = "[a-d]{5}|[x-z]{5}";
       //想要分开a-d和x-z只能单独写完整的限定
       content = "abcyx";
       System.out.println(content.matches(re)+"false");
       re = "^\\w*空+$";
       //在[]外使用的^,放在正则的开头表示匹配限定以^后面的字符开头,$放在正则的结尾表示以$前面的字符结尾
       content = "空";
       //^\\w*作为一组,表示以零个或多个\w开头,空+$作为一组,表示以一个或多个空结尾,所以空前面没有内容也符合^\\w*的要求
       System.out.println(content.matches(re)+"true");

       //常见正则表达式
       //邮箱

       re = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
       re = "([\\w&&[^_]]+[-|.]?)+[\\w&&[^_]]@([a-z0-9A-Z]+(-[a-z\\dA-Z]+)?\\.)+[a-zA-Z]{2,}";
       //IP地址
       re = "(25[0-5])|(2[0-4]\\d)|([0-1]\\d{2})|([1-9]?\\d)";
       //URL
       re = "[a-zA-Z]+://\\S*";
       re = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?";
       //身份证号码
       re = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})(\\d|X)$";
       re = "(^\\d{18}$)|(^\\d{15}$)";
   }
}

java正则表达式的使用的评论 (共 条)

分享到微博请遵守国家法律