RegularExpressions 字符串处理利器

    xiaoxiao2021-03-25  74

    正则表达式验证网站:https://regexr.com/ 正则表达式联系网站:https://alf.nu/RegexGolf 好文章:https://www.zhihu.com/question/48219401/answer/742444326?hb_wx_block=0


    java.util.regex.Matcher; java.util.regex.Pattern;

    参考:http://blog.csdn.net/hudie1234567/article/details/6642181

    public class Test { public static void main(String[] args) { p("abc".matches("..."));//true p("a8343a".replaceAll("\\d", "-"));//a----a Pattern p=Pattern.compile("[a-z]{3}"); Matcher m=p.matcher("fgh"); p(m.matches());//获取匹配结果 //true p("fgh".matches("[a-z]{3}"));//true //相当于上面三个语句 但上面的可以预编译效率高 p("-------------------"); //初步认识 . * + ? p("a".matches("."));//表示任何一个字符 //true p("aa".matches("aa"));//匹配第一个字符是a,最后一个字符也是a //true p("aaaa".matches("a*"));//表示a出现0次或多次 //true p("aaaa".matches("a+"));//表示a出现1次或多次 //true p("aaaa".matches("a?"));//表示a出现0次或1次 //false p("".matches("a*")); //true p("".matches("a?")); //true p("a".matches("a?")); //true p("214564564564545".matches("\\d{3,100}"));//表示数字至少3次但不超过100次 //true p("192.168.0.aaa".matches("\\{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));//1到3位数字.1到3位的数字.1到3位的数字.1到3位的数字 //false p("192".matches("[0-2][0-9][0-9]"));//第一个数字范围0-2,第二个数字范围0-9,第三个数字范围0-9 //true p("-------------------"); //范围 一个中括号表示匹配一个字符 p("a".matches("[abc]")); //匹配abc中的一个即可 //true p("a".matches("[^abc]")); //匹配除了abc外的一个字符 //false p("A".matches("[a-zA-Z]")); //匹配a到z或者是A到Z的一个字符 //true p("A".matches("[a-z]|[A-Z]")); //同上 p("A".matches("[a-z[A-Z]]")); //同上 p("R".matches("[A-Z&&[RFG]]")); //匹配A到Z且是 RFG三者之一的一个字符 //true p("-------------------"); //认识 \s \w \d \ p(" \n\r\t".matches("\\s{4}")); //匹配4个空格 //true p(" ".matches("\\S")); //匹配非空格 //false p("a_8".matches("\\w{3}")); //匹配3个字符 //true p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+")); //true p("\\".matches("\\\\")); //匹配反斜杠,在正则表达式中4个反斜杠表示一个反斜杠 //true p("-------------------"); //POSIX Style 不常用 p("a".matches("\\p{Lower}"));//true p("-------------------"); //boundary p("hello sir".matches("^h.*"));//以h开头,后面有0个或多个字符 //true p("hello sir".matches(".*ir$"));//有0个或多个字符,以ir结尾 //true p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));//以h开头,有a到z1个到3个,o,单词间隔,0个或多个字符 //true p("hellosir".matches("^h[a-z]{1,3}o\\b.*")); //false p("-------------------"); //white lines p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));//开头的是一个空白符但不是一个换行符,出现0次或多次,以换行符结束 //true p("aaa 8888c".matches(".*\\d{4}."));//0或多个字符,8位数字,1个字符 //true p("aaa 8888c".matches(".*\\b\\d{4}."));//0或多个字符,单词间隔,4个数字,1个字符 //true p("aaa8888c".matches(".*\\d{4}."));//true p("aaa8888c".matches(".*\\b\\d{4}."));//false p("-------------------"); //email p("ajfkdsjflksdjflsfjlk@sdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));//true p("-------------------"); //matches find lookingAt Pattern p1=Pattern.compile("\\d{3,5}"); String s="123-34345-234-00"; Matcher m1=p1.matcher(s); p(m1.matches());//false m1.reset(); p(m1.find());//找下一个匹配序列 //true 找到-时发现不匹配了 但只吃不吐,下面会从下个字符开始匹配 p(m1.start()+"--"+m1.end());//start是匹配的第一个位置,end是匹配字符的后一个位置 //0--3 p(m1.find());//true 从34345-234-00找看是否有匹配的 p(m1.start()+"--"+m1.end());//4--9 p(m1.find());//true 从234-00找看是否有匹配的 p(m1.start()+"--"+m1.end());//10--13 p(m1.find());//false 从00找看是否有匹配的 p(m.lookingAt());//每次都从头开始找 //true p("-------------------"); //replacement Pattern p2=Pattern.compile("java",Pattern.CASE_INSENSITIVE);//设置匹配模式为大小写不敏感 Matcher m2=p2.matcher("java Java jAva JaVa IloveJAVA you hateJavaasasfdsf"); while(m2.find()){ p(m2.group()); //java Java jAva JaVa JAVA Java } p(m2.replaceAll("JAVA"));//JAVA JAVA JAVA JAVA IloveJAVA you hateJAVAasasfdsf m2.reset(); //双数替换成小写,单数替换成大写 StringBuffer buf=new StringBuffer(); int i=0; while(m2.find()){ i++; if(i%2==0){ m2.appendReplacement(buf, "java"); }else{ m2.appendReplacement(buf, "JAVA"); } } m2.appendTail(buf); p(buf);//JAVA java JAVA java IloveJAVA you hatejavaasasfdsf p("-------------------"); //group 正则表达式的分组 几个小括号,就是分几组 是第几组,按左小括号出现的顺序排 Pattern p3=Pattern.compile("(\\d{3,5})([a-z]{2})"); String s3="123aa-34345bb-234cc-00"; Matcher m3=p3.matcher(s3); while(m3.find()){ p(m3.group(1));//只打印数字 即匹配第一组 } p("-------------------"); /* 区别: * Greedy quantifiers:贪婪的,从最大的范围开始匹配 * Reluctant quantifiers:不情愿的,从最小的范围开始匹配 * Possessive quantifiers:占有式的,从最大匹配,但不往外吐了,即不给后面组的正则项匹配的机会 */ Pattern p4=Pattern.compile("(.{3,10})[0-9]");//Greedy quantifiers String s4="aaaa5bbbb6"; Matcher m4=p4.matcher(s4); if(m4.find()){ p(m4.start()+"-"+m4.end());//0-10 }else{ p("not match!"); } Pattern p5=Pattern.compile("(.{3,10}?)[0-9]");//Reluctant quantifiers String s5="aaaa5bbbb6"; Matcher m5=p5.matcher(s5); if(m5.find()){ p(m5.start()+"-"+m5.end());//0-5 }else{ p("not match!"); } Pattern p6=Pattern.compile("(.{3,10}+)[0-9]");//Possessive quantifiers String s6="aaaa5bbbb6"; Matcher m6=p6.matcher(s6); if(m6.find()){ p(m6.start()+"-"+m6.end());//not match! }else{ p("not match!"); } p("-------------------"); //non-capturing groups Pattern p7=Pattern.compile(".{3}(?=a)");//匹配前面三个字符后面是a,但并不消耗a字符 String s7="444a666b"; Matcher m7=p7.matcher(s7); while(m7.find()){ p(m7.group());//444 } Pattern p8=Pattern.compile(".{3}(?!a)");//匹配前面三个字符后面不是a,但并不消耗a字符 String s8="444a666b"; Matcher m8=p8.matcher(s8); while(m8.find()){ p(m8.group());//44a 666 } p("-------------------"); //back refenrences 向前引用 Pattern p9=Pattern.compile("(\\d\\d)\\1");// \\1 表示第一个组匹配的字符串 String s9="1212"; Matcher m9=p9.matcher(s9); p(m9.matches());//true } public static void p(Object o){ System.out.println(o); } }

    抓取html文件的邮箱地址

    public class EmailSpider { public static void main(String[] args) { try { BufferedReader br=new BufferedReader(new FileReader("1.html")); String line=""; while((line=br.readLine())!=null){ parse(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void parse(String line){ Pattern p=Pattern.compile("[\\w[.-]]+@[\\w.[.-]]+\\.[\\w]+"); Matcher m=p.matcher(line); while(m.find()){ System.out.println(m.group()); } } }

    代码量检测器

    /* * 代码检测器 */ public class CodeCounter { static long normalLines=0;//正常行数 static long commentLines=0;//注释行数 static long whiteLines=0;//空白行数 public static void main(String[] args) { File f = new File("D:/Users/zxm/workspace/javademo/day19/src/day19"); File[] codeFiles = f.listFiles(); for (File child : codeFiles) { if (child.getName().matches(".*\\.java$")) {//如果是.java文件 parse(child); } } System.out.println("normalLines:" + normalLines); System.out.println("commentLines:" + commentLines); System.out.println("whileLines:" + whiteLines); } private static void parse(File f) { BufferedReader br=null; boolean comment=false; try{ br=new BufferedReader(new FileReader(f)); String line=""; while((line=br.readLine())!=null){//读到的不是空行 line=line.trim(); if(line.matches("^[\\s&&[^\\n]]*$")){ whiteLines++; }else if(line.startsWith("/*")&&line.endsWith("*/")){ commentLines++; }else if(line.startsWith("/*")&&!line.endsWith("*/")){ commentLines++; comment=true; }else if(true==comment){ commentLines++; if(line.endsWith("*/")){ comment=false; } }else if(line.startsWith("//")){ commentLines++; }else{ normalLines++; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(br!=null){ try{ br.close(); br=null; }catch(IOException e){ e.printStackTrace(); } } } } }
    转载请注明原文地址: https://ju.6miu.com/read-40310.html

    最新回复(0)