Java正則表達式經過java.util.regex包下的Pattern類與Matcher類實現
1.Pattern類用於建立一個正則表達式,也能夠說建立一個匹配模式,它的構造方法是私有的,不能夠直接建立,但能夠經過Pattern.complie(String regex)簡單工廠方法建立一個正則表達式.html
2.Matcher類的構造方法也是私有的,不能隨意建立,只能經過Pattern.matcher(CharSequence input)方法獲得該類的實例.Pattern類只能作一些簡單的匹配操做,java
要想獲得更強更便捷的正則匹配操做,那就須要將Pattern與Matcher一塊兒合做.Matcher類提供了對正則表達式的分組支持,以及對正則表達式的屢次匹配支持.正則表達式
Pattern類用於建立一個正則表達式,也能夠說建立一個匹配模式,它的構造方法是私有的,不能夠直接建立,但能夠經過Pattern.complie(String regex)簡單工廠方法建立一個正則表達式,將給定的正則表達式編譯到模式中.
pattern() 返回正則表達式的字符串形式,其實就是返回Pattern.complile(String regex)的regex參數
/** * Pattern類用於建立一個正則表達式,也能夠說建立一個匹配模式,它的構造方法是私有的,不能夠直接建立, * 但能夠經過Pattern.complie(String regex)簡單工廠方法建立一個正則表達式,將給定的正則表達式編譯到模式中, * pattern() 返回正則表達式的字符串形式,其實就是返回Pattern.complile(String regex)的regex參數 */ @Test public void test3(){ Pattern p=Pattern.compile("\\w+"); String str = p.pattern(); System.out.println(str);//返回 \w+ }
Pattern有一個split(CharSequence input)方法,用於分隔字符串,並返回一個String[],String.split(String regex)就是經過Pattern.split(CharSequence input)來實現的.
/** * Pattern有一個split(CharSequence input)方法,用於分隔字符串,並返回一個String[], * String.split(String regex)就是經過Pattern.split(CharSequence input)來實現的. */ @Test public void test4(){ Pattern p=Pattern.compile("\\d+"); String[] str=p.split("個人QQ是:456456個人電話是:0532214個人郵箱是:aaa@aaa.com"); for (String string : str) { System.out.println(string); /* 打印結果: 個人QQ是: 個人電話是: 個人郵箱是:aaa@aaa.com */ } }
Pattern.matches(String regex,CharSequence input)是一個靜態方法,用於快速匹配字符串,該方法適合用於只匹配一次,且匹配所有字符串.dom
@Test public void test5(){ System.out.println(Pattern.matches("\\d+","2223"));//返回true System.out.println(Pattern.matches("\\d+","2223aa"));//返回false,須要匹配到全部字符串才能返回true,這裏aa不能匹配到 System.out.println(Pattern.matches("\\d+","22bb23"));//返回false,須要匹配到全部字符串才能返回true,這裏bb不能匹配到 }
Matcher類的構造方法也是私有的,不能隨意建立,只能經過Pattern.matcher(CharSequence input)方法獲得該類的實例.Pattern類只能作一些簡單的匹配操做,要想獲得更強更便捷的正則匹配操做,那就須要將Pattern與Matcher一塊兒合做.url
Matcher類提供了對正則表達式的分組支持,以及對正則表達式的屢次匹配支持. spa
@Test public void test6(){ Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); m.pattern();//返回p 也就是返回該Matcher對象是由哪一個Pattern對象的建立的 }
Matcher類提供三個匹配操做方法,三個方法均返回boolean類型,當匹配到時返回true,沒匹配到則返回false,matches()對整個字符串進行匹配,只有整個字符串都匹配了才返回truecode
/** * Matcher類提供三個匹配操做方法,三個方法均返回boolean類型,當匹配到時返回true,沒匹配到則返回false * matches()對整個字符串進行匹配,只有整個字符串都匹配了才返回true */ @Test public void test7(){ Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); System.out.println( m.matches() );//返回false,由於bb不能被\d+匹配,致使整個字符串匹配未成功. Matcher m2=p.matcher("2223"); System.out.println( m2.matches() );//返回true,由於\d+匹配到了整個字符串 }
咱們如今回頭看一下Pattern.matches(String regex,CharSequence input),它與Pattern.compile(regex).matcher(input).matches() 等價!htm
lookingAt()對前面的字符串進行匹配,只有匹配到的字符串在最前面才返回true
/** * lookingAt()對前面的字符串進行匹配,只有匹配到的字符串在最前面才返回true */ @Test public void test8(){ 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 }
find()對字符串進行匹配,匹配到的字符串能夠在任何位置.對象
/** * find()對字符串進行匹配,匹配到的字符串能夠在任何位置. */ @Test public void test9(){ 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 }
@Test public void test10(){ Pattern p=Pattern.compile("\\d+"); Matcher matcher = p.matcher("aaa2223bb"); System.out.println( matcher.find() );//返回ture 匹配2223 System.out.println( matcher.start() );//返回3 System.out.println( matcher.end() );//返回7,返回的是2223後的索引號 System.out.println( matcher.group() ); //返回2223 System.out.println("============="); Matcher matcher2=p.matcher("2223bb"); System.out.println( matcher2.lookingAt() ); //返回true 匹配2223 System.out.println( matcher2.start() ); //返回0,因爲lookingAt()只能匹配前面的字符串,因此當使用lookingAt()匹配時,start()方法老是返回0 System.out.println( matcher2.end() ); //返回4 System.out.println( matcher2.group() ); //返回2223 }
start(),end(),group()均有一個重載方法它們是start(int i),end(int i),group(int i)專用於分組操做,Mathcer類還有一個groupCount()用於返回有多少組. blog
/** *說了這麼多,相信你們都明白了以上幾個方法的使用,該說說正則表達式的分組在java中是怎麼使用的. *start(),end(),group()均有一個重載方法它們是start(int i),end(int i),group(int i)專用於分組操做,Mathcer類還有一個groupCount()用於返回有多少組. */ @Test public void test11(){ Pattern p=Pattern.compile("([a-z]+)(\\d+)"); Matcher m=p.matcher("aaa2223bb"); m.find(); //匹配aaa2223 m.groupCount(); //返回2,由於有2組 m.start(1); //返回0 返回第一組匹配到的子字符串在字符串中的索引號 m.start(2); //返回3 m.end(1); //返回3 返回第一組匹配到的子字符串的最後一個字符在字符串中的索引位置. m.end(2); //返回7 m.group(1); //返回aaa,返回第一組匹配到的子字符串 m.group(2); //返回2223,返回第二組匹配到的子字符串 }
/** * 如今咱們使用一下稍微高級點的正則匹配操做,例若有一段文本,裏面有不少數字, * 並且這些數字是分開的,咱們如今要將文本中全部數字都取出來,利用java的正則操做是那麼的簡單. */ @Test public void test12(){ Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("個人QQ是:456456 個人電話是:0532214 個人郵箱是:aaa123@aaa.com"); while(m.find()) { System.out.println(m.group()); /* 打印輸出 456456 0532214 123 */ } }
/** * 如今你們應該知道,每次執行匹配操做後start(),end(),group()三個方法的值都會改變, * 改變成匹配到的子字符串的信息,以及它們的重載方法,也會改變成相應的信息. * 注意:只有當匹配操做成功,纔可使用start(),end(),group()三個方法,不然會拋出java.lang.IllegalStateException, * 也就是當matches(),lookingAt(),find()其中任意一個方法返回true時,纔可使用. */ @Test public void test13(){ Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("個人QQ是:456456 個人電話是:0532214 個人郵箱是:aaa123@aaa.com"); while(m.find()) { System.out.println(m.group()); System.out.print("start:"+m.start()); System.out.println(" end:"+m.end()); /* 打印輸出 456456 start:6 end:12 0532214 start:19 end:26 123 start:36 end:39 */ } }
@Test public void test1() { Pattern pattern = Pattern.compile("頁面下載失敗\\.url:\\[http://[a-z0-9]+\\.(.+)/.+\\]\\.當前時間戳:\\[([0-9]+)\\]"); Matcher matcher = pattern.matcher("頁面下載失敗.url:[http://item.jd.com/15626278.html].當前時間戳:[1471415298943]"); if(matcher.find()){ String top_domain = matcher.group(1); String curr_time = matcher.group(2); System.out.println(top_domain+"--"+"--"+curr_time);//jd.com----1471415298943 } } @Test public void test2(){ String url = "https://item.jd.com/698763154.html"; Pattern pattern = Pattern.compile("https://item.jd.com/([0-9]+).html"); Matcher matcher = pattern.matcher(url); if(matcher.find()){ System.out.println(matcher.group(1));//698763154 System.out.println(matcher.group(0));//https://item.jd.com/698763154.html } }