javaRegex

來源:互聯網
上載者:User

標籤:邏輯運算   3.1   compile   表達   http   運算式   表示   index   system   

1、什麼是javaRegex

        1、RegularExpressions(Regex) Regex是一種用來描述一定數量文本的模式,常用於:字串匹配、尋找和替換。
        2、Regex是字串處理的利器,它是一種描述字串模式的式子,一個Regex的核心價值就是匹配一個字串。

        3、樣本:

        // 定義匹配規則 需要匯入Regex的包import java.util.regex.*;        Pattern p=Pattern.compile("\\d");        // 給出要匹配的東西 需要匯入Regex的包 import java.util.regex.Matcher;         Matcher m=p.matcher("1");            //matches()方法給出匹配結果-匹配:true,不匹配:false         System.out.println(m.matches());    

2、Regex的規則

    2.1 常用Regex

2.2 數量表示

2.3  邏輯運算子

3、案例參考

       3.1 電子郵件:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$(標準) 參考 ^\[email protected]\w+\.\w+$

       3.2 中國電話號碼:^(\(\d{3}\)|\d{3}-)?\d{8} (標準) 參考 ^0[0-9]{2,3}-[1-9][0-9]{6,7}$

       3.3 驗證內部tomcat伺服器的地址是否符合要求    

        Pattern p = Pattern.compile("http://(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d{1,5})/\\w+\\.\\w+");        Matcher m = p.matcher("http://127.0.0.1:8081/index.html");        if (m.find())        {            System.out.println("該伺服器的地址是:"+m.group(1));            System.out.println("該連接埠是:"+m.group(2));        }
輸出結果:

                   該伺服器的地址是:127.0.0.1
                   該連接埠是:8081

 

 

javaRegex

聯繫我們

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