標籤:
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--待續