Regex概述與JAVA中Regex的應用

來源:互聯網
上載者:User

from:http://blog.csdn.net/xyydyyqf/article/details/9153313

編程或者電腦使用過程中,經常需要對字串進行 匹配,尋找,替換,判斷。如果單純用代碼 if () ,whlie 什麼的進行比較複雜麻煩。Regex是一種強大靈活的文本處理工具,專門對字串進行匹配,尋找,替換,判斷。

Regex的各種匹配符

[java]
view plaincopy
  1. // Regex的各種匹配符  
  2.   
  3. ^              從行的開始位置開始匹配  
  4. $              匹配到行的結束位置  
  5. \b             匹配單詞的開始或結束位置  
  6.   
  7. .              匹配除分行符號的所有任一字元  
  8. \w             匹配單字(a-z,A-Z,0-9,底線)    
  9. \W             匹配非單字字元  
  10. \s             匹配空白字元  
  11. \S             匹配非空白字元  
  12. \d             匹配數字字元(0-9)     
  13. \D             匹配非數字字元  
  14.   
  15. *              匹配前面運算式零次或多次,等價於 {0,}  
  16. +              匹配前面運算式一次或多次,等價於 {1,}  
  17. ?              匹配前面運算式零次或一次,等價於{0,1}   
  18. {n}            匹配前面的運算式n次,n是一個非負整數  
  19. {n,}           匹配前面的運算式至少n次,n是一個非負整數  
  20. {n,m}          匹配前面的運算式 n-m 次, nm都是非負整數  
  21.   
  22. []             字元類,如[123]表示有123中任意一個數字,[1-9]表示1到9中任意一個數字  
  23. ()             分組,匹配括弧裡的整體,(java)+ 就是匹配一個"java"至少出現一次的字串  
  24. ^              反義匹配,用在判斷條件前。 例如 .*[^\d]\.java$ 就是匹配最後一位非數位java檔案  
  25. |              邏輯或  
  26. \              逸出字元,和程式設計語言裡的一樣  


瞭解了這些匹配符,就可以開始寫匹配式,很多網站提交資訊的時候說填寫的什麼資訊不規範就是正則匹配式進行判斷。

手機號碼驗證:國內手機都是13,15,18開頭的11位元字,據此來進行匹配驗證

匹配式:    \b1[358]\d{9}\b    開頭與結尾寫\b,號碼開始是1,第二位是3、5、8中的一個數字,剩下九位是數字


[java]
view plaincopy
  1. boolean check = false;  
  2. String regex = "\b1[358]\d{9}\b";  
  3. String input = "19100390888";  
  4. Pattern pattern = Pattern.compile(regex) //編譯Regex  
  5. Matcher matcher = pattern.matcher(input) //進行匹配  
  6. if (matcher.matches()) check = true;  
  7. return check;  

身份證驗證:前六位地區碼(數字),中間八位生日(數字),末尾四位標識碼(數字)。生日年開頭是19或者2,月01-12,日01-31。

匹配式: ^\d{6}((19)|(2\d)\d{2}((0\d)|(1[012]))(([0-2]\d)|(3[01]))\d{4})$


[java]
view plaincopy
  1. boolean check = false;  
  2. String regex = "^\d{6}((19)|(2\d)\d{2}((0\d)|(1[012]))(([0-2]\d)|(3[01]))\d{4})$";  
  3. String input = "19100390888";  
  4. Pattern pattern = Pattern.compile(regex) //編譯Regex  
  5. Matcher matcher = pattern.matcher(input) //進行匹配  
  6. if (matcher.matches()) check = true;  
  7. return check;  

郵箱驗證:前面任意位元非Null 字元,必須帶@,末尾必須有.com或者.cn

匹配式: \w+@\w+\.(com)|(cn)

[java]
view plaincopy
  1. boolean check = false;  
  2. String regex = "\w+@\w+\.(com)|(cn)";  
  3. String input = "19100390888";  
  4. Pattern pattern = Pattern.compile(regex) //編譯Regex  
  5. Matcher matcher = pattern.matcher(input) //進行匹配  
  6. if (matcher.matches()) check = true;  
  7. return check;  


/*

******逸雨清風 出品

******http://blog.csdn.net/xyydyyqf

******2013.6.23

*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.