Java 正则应用总结

来源:互联网 发布:设计模式 java 编辑:程序博客网 时间:2024/05/29 13:21
/*//分组//测试1:结果为3行 Pattern p = Pattern.compile("[/]+"); String[] result = p.split("Kevin has seen《LEON》seveal times,because it is a good film."+ "/ 凯文已经看过《这个杀手不太冷》几次了,因为它是一部" + "好电影。/名词:凯文。"); for (int i = 0; i <result.length; i++) { System.out.println(result[i]); }   //测试2:结果为2行String[] result2 = p.split("Kevin has seen《LEON》seveal times,because it is a good film."+ "/ 凯文已经看过《这个杀手不太冷》几次了,因为它是一部" + "好电影。/名词:凯文。", 2); for (int i = 0; i < result2.length; i++) { System.out.println(result2[i]); }*/ //测试3:使用正则表达式判断是否是某种类型的数据 m.matches() 全文查询/*Pattern p = Pattern.compile("\\d+");Matcher m = p.matcher("1234532323");if (m.matches()) {System.out.println("正确,是由数字组成的");} else {System.out.println("错误,不是由数字组成的");}*///------------------------完整总结开始//0/*Pattern p=Pattern.compile("\\w+"); System.out.println(p.pattern()); //返回 \w+ *///1 以数字来区分/*Pattern p=Pattern.compile("\\d+"); String[] str=p.split("我的QQ是:456456我的电话是:0532214我的邮箱是:aaa@aaa.com"); printMessage(str); //结果:str[0]="我的QQ是:" str[1]="我的电话是:" str[2]="我的邮箱是:aaa@aaa.com" ,这是以数字来分割*//*//2boolean bool = Pattern.matches("\\d+","2223");//返回true System.out.println(bool); Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,这里aa不能匹配到 Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,这里bb不能匹配到 *//*//3Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); System.out.println(m.pattern()); // \d+*///4 Matcher.matches()  Matcher.lookingAt()  Matcher.find() //matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true /*Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); System.out.println(m.matches()); //返回false Matcher m2=p.matcher("2223"); System.out.println(m2.matches());//返回true,因为\d+匹配到了整个字符串 *///Matcher.lookingAt()//lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true  /*Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); System.out.println(m.lookingAt());//返回true,因为\d+匹配到了前面的22 Matcher m2=p.matcher("aa2223"); System.out.println(m2.lookingAt());//返回false,因为\d+不能匹配前面的aa *///Matcher.find() //find()对字符串进行匹配,匹配到的字符串可以在任何位置. /*Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); System.out.println(m.find()); //返回true Matcher m2=p.matcher("aa2223"); System.out.println(m2.find()); //返回true Matcher m3=p.matcher("aa2223bb"); System.out.println(m3.find()); //返回true Matcher m4=p.matcher("aabb"); System.out.println(m4.find()); //返回false *///5.Mathcer.start()/ Matcher.end()/ Matcher.group() //当使用matches(),lookingAt(),find()执行匹配操作后,就可以利用以上三个方法得到更详细的信息. //start()返回匹配到的子字符串在字符串中的索引位置. //end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置. //group()返回匹配到的子字符串 //示例1/*System.out.println("=================1");Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("aaa2223bb556"); //匹配2223System.out.println(m.find()); //trueSystem.out.println(m.start()); //返回3 System.out.println(m.end()); //返回7,返回的是2223后的索引号System.out.println(m.group()); //返回2223 //匹配556System.out.println(m.find()); //trueSystem.out.println(m.start()); //返回9 System.out.println(m.end()); //返回12,返回的是556后的索引号System.out.println(m.group()); //返回556System.out.println("===================2");Matcher m2=p.matcher("2223bb556"); System.out.println(m2.lookingAt()); //匹配2223   从头开始拿System.out.println(m2.start()); //返回0,由于lookingAt()只能匹配前面的字符串,所以当使用lookingAt()匹配时,start()方法总是返回0   System.out.println(m2.end()); //返回4 System.out.println(m2.group()); ////返回2223  System.out.println("===================3");Matcher m3=p.matcher("2223"); //如果Matcher m3=p.matcher("2223bb"); 那么下面的方法出错,因为不匹配返回falseSystem.out.println(m3.matches()); //匹配整个字符串   System.out.println(m3.start()); //返回0  System.out.println(m3.end()); //返回3,原因相信大家也清楚了,因为matches()需要匹配所有字符串   System.out.println(m3.group()); //返回2223*/   /*System.out.println("===================4");Pattern p=Pattern.compile("([a-z]+)(\\d+)");   Matcher m=p.matcher("aaa2223bb"); System.out.println(m.find()); //匹配aaa2223,find是拿到引号中的内容    System.out.println(m.groupCount());  //返回2,因为有2组子模式 System.out.println(m.start(1));   //返回0 第一组子模式的开始System.out.println(m.start(2));   //返回3 第二组子模式的开始System.out.println(m.end(1));   //返回3 第一组子模式的结束System.out.println(m.end(2));   //返回7 第二组子模式的结束System.out.println(m.group(1));   //返回aaa,group(1) 表示返回本次find中第一组子模式System.out.println(m.group(2));   //返回2223,group(2) 表示返回本次find中第二组子模式*///找出里面的数字/*Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("我的QQ是:456456 我的电话是:0532214 我的邮箱是:aaa123@aaa.com"); while(m.find()) {      System.out.println(m.group()); } */

1 0
原创粉丝点击