2015.9.14 博逸楼510,2015.9.14博逸510,package java
分享于 点击 42857 次 点评:83
2015.9.14 博逸楼510,2015.9.14博逸510,package java
package javaapplication3;import java.util.regex.Matcher;import java.util.regex.Pattern;public class JavaApplication3 { public static void main(String[] args) { // TODO code application logic here /* String s1= "pattern"; Pattern mypatten = Pattern.compile(s1); String s2=mypatten.pattern(); System.out.println(s2); */ /* Pattern pattern = Pattern.compile("John"); Matcher matcher = pattern.matcher("John does this,and John asdasdasda"); StringBuffer s3 = new StringBuffer(); while(matcher.find()) { matcher.appendReplacement(s3, "sam"); System.out.println(s3.toString()); } */ /* Pattern pattern = Pattern.compile("John"); Matcher matcher = pattern.matcher("John does this,and John does that"); StringBuffer s3 = new StringBuffer(); while(matcher.find()) { matcher.appendReplacement(s3, "sam"); System.out.println(s3.toString()); } matcher.appendTail(s3); System.out.println(s3.toString()); */ //Using regex check how many times "is" has been written in a string? //"He is going to meet his uncle whi is working in Beging" /* Pattern pattern=Pattern.compile(""); Matcher matcher=pattern.matcher("John does this,and John does that"); String s2=matcher.replaceAll("sam"); System.out.println("replaceAll="+s2); */ /* String str = "He is going to meet his uncle whi is working in Beging"; Pattern pattern =Pattern.compile("is"); Matcher matcher = pattern.matcher(str); int count=0; while(matcher.find()) {count++;} System.out.println(count);*/ //201440706666 String str ="201440706666"; Pattern pattern = Pattern.compile("20144070[0-9]{4}"); Matcher matcher = pattern.matcher(str); if(matcher.matches()) System.out.println("ok"); else System.out.println("bu ok"); }}
用户点评