赞
踩
String s="abc aba acd,opq,lol,ppq"; Pattern p=Pattern.compile("[a-z, ]+"); Matcher m=p.matcher(s); System.out.println(m.matches()); |
结果:true |
Pattern p=Pattern.compile("\\w+"); Matcher m=p.matcher(s); while (m.find()){ System.out.print(" "+m.group()); } |
abc aba acd opq lol ppq |
Pattern p = Pattern.compile("\\w+"); Matcher m = p.matcher(s); while (m.find()) { for (int i = 0; i <= m.groupCount(); i++) { System.out.print(m.group(i)+" "); } } |
abc aba acd opq lol ppq |
String s = "abc(两个空格)aba acd,opq,lol,ppq"; Pattern p = Pattern.compile("\\w+"); Matcher m = p.matcher(s); while (m.find()) { System.out.println("data:"+m.group()+";start:"+m.start()+";end:"+m.end()); } |
data:abc;start:0;end:3 data:aba;start:5;end:8 data:acd;start:9;end:12 data:opq;start:13;end:16 data:lol;start:17;end:20 data:ppq;start:21;end:24 |
String s = "abc aba acd,opq,lol,ppq"; Pattern p = Pattern.compile("\\w"); Matcher m = p.matcher(s); System.out.println(m.lookingAt()); |
true |
Pattern p = Pattern.compile("^java",Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); String s = "java has regex\nJava has regx\nJAVA has pretty\nlalla in Java"; Matcher m=p.matcher(s); while (m.find()){ System.out.println(m.group()); } |
java Java JAVA |
String s = "abc!!aba!!acd!!opq!!haha"; String[] st = Pattern.compile("!!").split(s); System.out.println(Arrays.toString(st)); String[] st1 = Pattern.compile("!!").split(s,3); System.out.println(Arrays.toString(st1)); |
[abc, aba, acd, opq, haha] [abc, aba, acd!!opq!!haha] |
String s=" aaasf. hsfhuf ijdigju,ihuih chbcjx ch\n" + " dew.yhgy ehdywhf uila ej,lk dfn,jk\n" + " leh ruiae, hui, lfhl kndfjk."; System.out.println(s); Matcher m=Pattern.compile(".*",Pattern.DOTALL).matcher(s); if (m.find()){ s=m.group(0); } //将两个空格替换成一个空格 s=s.replaceAll(" {2,}"," "); //删除每行开头所有空格, s=s.replaceAll(" (?m)^ +",""); //将第一个匹配到的替换为(VV) s=s.replaceFirst("[aeiou]","(VV)"); StringBuffer sb=new StringBuffer(); Pattern p=Pattern.compile("[aeiou]");//正则匹配 Matcher mm=p.matcher(s); while (mm.find()){ mm.appendReplacement(sb,mm.group().toUpperCase()); } mm.appendTail(sb);//添加到StringBuffer里 System.out.println("================================"); System.out.println("sb:"+sb); |
aaasf. hsfhuf ijdigju,ihuih chbcjx ch dew.yhgy ehdywhf uila ej,lk dfn,jk leh ruiae, hui, lfhl kndfjk. ================================ sb: (VV)AAsf. hsfhUf IjdIgjU,IhUIh chbcjx ch dEw.yhgy Ehdywhf UIlA Ej,lk dfn,jk lEh rUIAE, hUI, lfhl kndfjk. |
String s1="fix the rug with bags"; String s2="fix the rig with rags"; Matcher m=Pattern.compile("[frb][aiu][gx]").matcher(s1); while (m.find()) System.out.print(m.group()+" "); System.out.println(); m.reset(s2); while (m.find()) System.out.print(m.group()+" "); |
fix rug bag fix rig rag |
final String s="215.2.43.111@02/10/2015\n"+ "125.3.53.134@02/10/2015\n"+ "244.2.41.121@02/10/2015\n"+ "232.4.42.122@02/10/2015\n"+ "344.6.23.101@02/10/2015\n"+ "[Next log section with different data format]"; Scanner scanner=new Scanner(s); String pattern="(\\d+.\\d+.\\d+.\\d+)@(\\d+/\\d+/\\d+)"; while (scanner.hasNext(pattern)){ scanner.next(pattern); MatchResult match=scanner.match(); String ip=match.group(1); String date=match.group(2); System.out.printf("Threat on %s from %s \n",ip,date); } |
Threat on 25.2.43.111 from 02/10/2015 Threat on 125.3.53.134 from 02/10/2015 Threat on 244.2.41.121 from 02/10/2015 Threat on 232.4.42.122 from 02/10/2015 Threat on 344.6.23.101 from 02/10/2015 |
final String s="215.2.43.111@02/10/2015\n"+ "125.3.53.134@02,10/2015\n"+ "244.2.41.121@02,10/2015\n"+ "232.4.42.122@02,10/2015\n"+ "344.6.23.101@02,10/2015\n"+ "[Next log section with different data format]"; Scanner scanner=new Scanner(s); scanner.useDelimiter(","); System.out.println("delimiter:"+scanner.delimiter()); String pattern="(\\d+.\\d+.\\d+.\\d+)@(\\d+,\\d+/\\d+)"; while (scanner.hasNext(pattern)){ scanner.next(pattern); MatchResult match=scanner.match(); String ip=match.group(1); String date=match.group(2); System.out.printf("Threat on %s from %s \n",ip,date); } |
, |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。