JAVA基礎--Regex

來源:互聯網
上載者:User

標籤:

.  any character

\d  A digit 0-9

\D  a non-digit [^0-9]

\s  a whitespace character, 空白字元

\S  a non-whitespace character

\w  a word character   [a-zA-Z0-9]

\W  a non-word character

package DAO;import java.util.regex.Matcher;import java.util.regex.Pattern;public class TestReg {public static void main(String[] args) throws Exception{/*System.out.println("abc".matches("..."));  //true          ctrl+shift+/ 注釋System.out.println("a898938593a".replaceAll("\\d","-"));  //a---------aPattern p = Pattern.compile("[a-z]{3}");    //匹配具有3個字元的字串Matcher m =  p.matcher("abc");System.out.println(m.matches());          //true*///初步認識 . * + ?, meta characters/*p("a".matches(".")); // true p("aa".matches("aa")); // true p("aaaa".matches("a*"));   // true 0或者np("aaaa".matches("a+")); // true 1或者np("aaaa".matches("a?")); // false 0或者1p("".matches("a*"));    // true 0或者np("".matches("a+")); // true 1或者np("".matches("a?"));    // true 0或者1p("215454545454545".matches("\\d{3,100}"));    // true  3~100個p("192.168.000.111".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));           //truep("192".matches("[0-2][0-9][0-9]"));  //true*///範圍/*p("a".matches("[abc]"));       // true  []取出一個字元p("a".matches("[^abc]"));       // false  []取出一個字元p("A".matches("[a-zA-Z]"));       // true  []取出一個字元p("A".matches("[a-z]|[A-Z]"));       // true  []取出一個字元p("A".matches("[a-z[A-Z]]"));       // true  []取出一個字元p("R".matches("[A-Z&&|[RFG]]"));       // true  []取出一個字元*///認識 \s \w \d/*p(" \n\r\t".matches("\\s{4}"));  //true  4個空白字元p(" ".matches("S"));   //false  S代表非空白字元p("a_8".matches("\\w{3}"));  //truep("abc888&^%".matches("[a-z]{1,3}\\d+[&^$%]+"));  //true   1-3個字母, 1或n個數字, [&^$%]裡任意1或n個p("\\".matches("\\\\"));  //Regex裡 2個\\只代表一個逸出字元*///POSIX Style //p("a".matches("\\p{Lower}"));//boundary/*p("hello sir".matches("^h.*"));  //truep("hello sir".matches(".*ir$"));  //truep("hello sir".matches("^h[a-z]{1,3}o\\b.*"));  //truep("hellosir".matches("^h[a-z]{1,3}o\\b.*"));  //true  \b是空格, 換行等單詞邊界*///white lines 空白行//p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));  //true 開頭是空白符, 而且不是斷行符號, 0或n次, 後面緊跟著分行符號//practice/*p("aaa 8888c".matches(".*\\d{4}."));  //true  0或n個字母, 4位元字, 然後有一個字母p("aaa 8888c".matches(".*\\b\\d{4}."));  //true  0或n個字母, 4位元字前面有個單詞邊界, 然後有一個字母p("aaa8888c".matches(".*\\d{4}."));  //true  0或n個字母, 4位元字, 然後有一個字母p("aaa8888c".matches(".*\\b\\d{4}."));  //false  0或n個字母, 4位元字前面沒有單詞邊界, 然後有一個字母*///email//p("[email protected]".matches("[\\w[.-]][email protected][\\w[.-]]+\\.[\\w]+"));  //true//matches  find lookingAt    start  end       matches每次找整個串, find找字串, lookingAt每次都從頭找/*Pattern p = Pattern.compile("\\d{3,5}");String s = "123-34345-234-00";Matcher m = p.matcher(s);p(m.matches()); //把123-給吃了, 所以要reset以下. 否則, 如果不reset的話, 只有2個true 34345  234m.reset();      //為了把matches吃掉的字元恢複p(m.find());  //true 找一個匹配的字串 123p(m.start()+"-"+m.end());  //0-3p(m.find()); //true 找一個匹配的字串 34345p(m.start()+"-"+m.end());  //4-9p(m.find());//true找一個匹配的字串234p(m.start()+"-"+m.end());  //10-13p(m.find());  // false 00p(m.lookingAt());  //true  只從head開始找, 所以是123p(m.lookingAt());  //true  只從head開始找, 所以是123p(m.lookingAt());  //true  只從head開始找, 所以是123p(m.lookingAt());  //true  只從head開始找, 所以是123*///replacementPattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava");StringBuffer buf = new StringBuffer();int i=0;/*while(m.find()){p(m.group());  //列出所有java字樣的}*/while(m.find()){i++;if(i%2==0){m.appendReplacement(buf, "java");   //偶數都是java}else{m.appendReplacement(buf, "JAVA");//奇數都是JAVA}}m.appendTail(buf);p(buf);  //列出所有java字樣的}public static void p(Object o){System.out.println(o);}}

  

JAVA基礎--Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.