手機號碼 Regex

來源:互聯網
上載者:User

標籤:log   att   family   ack   sof   port   模式   regex   括弧   


代碼如下:

1 package TestRegex; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public class Test01 { 7 /** 8 * 手機號碼Regex:"^[1][3,5,7,8][0-9]\\d{8}$" 9 * ^:正則開始10 * $:正則結束11 * [1]:手機號碼第一個數必須為112 * [3,5,7,8]:手機號碼第二個數必須為:括弧內數字13 * [0-9]\\d{8}:最後的數可以為0-9的任意數 :{8}:表示恰好9個14 * \:轉換符15 * \d:表示前面的數為數字0-916 * X{n}:恰好為n數 注意:從零開始 17 */18 public static void main(String[] args) {19 //手機號碼Regex20 21 String str="^[1][3,5,7,8][0-9]\\d{8}$";22 //正確的手機號碼23 String s = "15188888888";24 //錯誤手機號碼 ,因為開頭是2,25 String s2 ="25123456789";26 27 /**28 * Pattern為模式類型,29 * compile(正則)方法先行編譯正則,30 * 得到一個Matcher對象31 */32 Pattern p =Pattern.compile(str);33 //Pattern中的matcher()方法傳入要匹配的字串與正則進行匹配i34 Matcher m=p.matcher(s);35 Matcher m2=p.matcher(s2);36 //Matcher類中的matches()方法判斷是否匹配成功37 boolean bo= m.matches();38 boolean bo2= m2.matches();39 //輸出匹配結果:true為成功,false為失敗40 System.out.println(bo);41 System.out.println(bo2);42 }43 }

 運行結果:

 

手機號碼 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.