JAVA中的Regex--待續

來源:互聯網
上載者:User

標籤:

1、關於“\”,在JAVA中的Regex中的不同;

    在其他語言中"\\"表示為:我想要在Regex中插入一個普通的反斜線;

    在Java中“\\”表示為:我想要插入一個Regex反斜線;

    eg:驗證整數的Regex為\\d; 如果想要插入一個反斜線則為:\\\\ ;

    如果是分行符號和定位字元則為\n 和\t ;

2、特殊字元

 

Greedy 數量詞
X? X,一次或一次也沒有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超過 m 次
 
Reluctant 數量詞
X?? X,一次或一次也沒有
X*? X,零次或多次
X+? X,一次或多次
X{n}? X,恰好 n 次
X{n,}? X,至少 n 次
X{n,m}? X,至少 n 次,但是不超過 m 次
 
Possessive 數量詞
X?+ X,一次或一次也沒有
X*+ X,零次或多次
X++ X,一次或多次
X{n}+ X,恰好 n 次
X{n,}+ X,至少 n 次
X{n,m}+ X,至少 n 次,但是不超過 m 次

示範代碼:

package com.st.day20150525;public class StringTest03 {        public static String message ="The class String includes methods for examining "            + "individual characters of the sequence, for comparing strings, "            + "for searching strings, for extracting substrings, and for "            + "creating a copy of a string with all characters translated to "            + "uppercase or to lowercase. Case mapping is based on the Unicode "            + "Standard version specified by the Character class.";        public static void main(String[] args) {        //1、使用String中的內建的功能,進行字元的嚴重        System.out.println("1243".matches("-?\\d+"));        System.out.println("+911".matches("-?\\d+"));        System.out.println("+911".matches("(-|\\+)?\\d+"));                //2、使用String.split() 將字串從Regex匹配的地方分開        String[] str01 = message.split("of|to");    // 從of 或者 to 的地方進行分開        for (int i = 0; i < str01.length; i++) {            System.out.println("第"+i+": "+str01[i]);        }        String[] str02 = message.split("\\W");     // 從空格的地方進行分開        for (int i = 0; i < str02.length; i++) {            System.out.print("第"+i+": "+str02[i]+"  ");        }    }}

 

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.