原文链接:https://www.f2er.com/regex/363201.html
- importjava.util.regex.Matcher;
- importjava.util.regex.Pattern;
- /**
- *正则表达式:字符串的处理利器
- *@authorkenan
- *
- */
- publicclassRegularExpressions{
- publicstaticvoidmain(String[]args){
- //在正则表达式中.代表一个字符
- //System.out.println("abc".matches("..."));
- //\d代表一个数字:使用表达式进行匹配替换
- //System.out.println("ab12356c".replaceAll("\\d","-"));
- //这个是我们平时的用法
- //System.out.println("ab12346c".replaceAll("123","---"));
- //编译一个正则表达式
- //Patternp=Pattern.compile("[a-z]{3}");
- //匹配一个字符串,返回一个Matcher对象
- //Matcherm=p.matcher("abc");
- //System.out.println(m.matches());
- //.:一个字符
- //*:0或多个字符
- //+:1或者多个
- //?:0或者1个
- //System.out.println("a".matches("."));
- //System.out.println("aa".matches("aa"));
- //System.out.println("aaaa".matches("a*"));
- //System.out.println("".matches("a*"));
- //System.out.println("aaa".matches("a?"));
- //System.out.println("".matches("a?"));
- //System.out.println("a".matches("a?"));
- //System.out.println("123456".matches("\\d{3,100}"));
- //System.out.println("12".matches("\\d{3,100}"));
- //System.out.println("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}"));
- //System.out.println("192.168.0.2".matches("\\d{1,3}"));
- //System.out.println("192".matches("[0-2][0-9][0-9]"));
- //System.out.println("192".matches("[0-9]"));
- //范围匹配
- //System.out.println("a".matches("[abc]"));
- //System.out.println("a".matches("[^abc]"));
- //System.out.println("A".matches("[a-zA-Z1-9]"));
- //System.out.println("3".matches("[a-zA-Z1-9]"));
- //System.out.println("A".matches("[a-z]|[A-Z]"));
- //System.out.println("R".matches("[A-Z&&[RFG]]"));
- //认识\s\w\d\
- //\d一个数字[0-9]
- //\D上面取反
- //\s一个空字符[\t\n\r\f\x0B]
- //\S上面取反
- //\w一个字符[a-zA-Z_0-9]
- //\W出了上面的字符以为的其他字符
- //\\代表一个\
- //System.out.println("\n\r\t".matches("\\s{1,4}"));
- //System.out.println("".matches("\\S"));
- //System.out.println("a_8".matches("\\w{1,3}"));
- //System.out.println("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));
- //System.out.println("\\".matches("\\\\"));
- //PosixStyle
- //System.out.println("a".matches("\\p{Lower}"));
- //边界处理
- //^在[]里代表取反在外面代表字符开头
- //$代表以什么结尾
- //\b代表单词结束
- //System.out.println("hellosir".matches("^h.*"));
- //System.out.println("hellosir".matches(".*ir$"));
- //System.out.println("hellosirr".matches(".*ir$"));
- //System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
- //System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
- //System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
- //System.out.println("hello&sir".matches("^h[a-z]{1,3}o\\b.*"));
- //System.out.println("hello.sir".matches("^h[a-z]{1,3}o\\b.*"));
- //找出空白行
- //System.out.println("\n".matches("^[\\s&&[^\\n]]*\\n$"));
- //System.out.println("\n".matches("^[\\s&&[^\\n]]*\\n$"));
- //System.out.println("soukenan@qq.com".matches("[\\w.-]+@[\\w.-]+\\.[\\w]+"));
- //matchesfromlookingat
- //Patternp=Pattern.compile("\\d{3,5}");
- //Strings="123-45674-234-00";
- //Matcherm=p.matcher(s);
- //System.out.println(m.matches());//false//从前往后匹配
- //m.reset();//把光标重置
- //System.out.println(m.find());//true//从当前的标志开始查找
- //System.out.println(m.start()+":"+m.end());
- //System.out.println(m.find());//true查找匹配的字串
- //System.out.println(m.start()+":"+m.end());
- //System.out.println(m.find());//true
- //System.out.println(m.start()+":"+m.end());
- //System.out.println(m.find());//false
- //
- //System.out.println(m.lookingAt());//true//从头开始看看是否匹配
- //System.out.println(m.lookingAt());//true
- //System.out.println(m.lookingAt());//true
- //System.out.println(m.lookingAt());//true
- //replacement字符串的替换
- //Patternp=null;
- //p=Pattern.compile("java");
- //p=Pattern.compile("java",Pattern.CASE_INSENSITIVE);//忽略大小写
- //Matcherm=p.matcher("javaJavaJaVajavA,iloveJAVA!");
- //while(m.find()){
- //System.out.println(m.group());
- //}
- //System.out.println(m.replaceAll("JAVA"));//把找到的匹配字符串转换成JAVA
- //StringBufferbuf=newStringBuffer();
- //inti=0;
- //while(m.find()){
- //i++;
- //if(i%2==0){
- //m.appendReplacement(buf,"java");//偶数转换成java
- //}else{
- //m.appendReplacement(buf,"JAVA");//奇数转换成JAVA
- //}
- //}
- //m.appendTail(buf);//把尾部字符串添加上去
- //System.out.println(buf);
- //group分组
- //()这个是用来分组的
- Patternp=Pattern.compile("(\\d{3,5})([a-z]{2})");
- Strings="123aa-3456bb-234cc-00";
- Matcherm=p.matcher(s);
- while(m.find()){
- System.out.println(m.group(1)+":"+m.group(2));
- }
- }
- }