申明:正則表達式能夠大大的簡化代碼,不過對於看不懂的人來講,那也只能罵娘。切身體會,因此感受有必要擼下來正則表達式!(後期會不斷添加各類正則判斷)java
基本概念很少說我,直接上例子,經過例子說明吧。正則表達式
1、正則基礎sql
Demo1:express
在java中對反斜線\的處理與其它語言不一樣,在其它語言中,\\表示「我想要在正則表達式中插入一個普通的(字面上的)反斜線」,請不要給它任何特殊的意義。而在java中,\\的意思是「我要插入一個正則表達式的反斜線,因此其後的寫符具備特殊的意義。"例如,若是我想要表示一位數字,那麼正則表達式應該是\\d。若是你想插入一個普通的反斜線,則應該這樣\\\\,不過換行和製表之類的東西只須要使用單反斜線:\n\tapi
package com.rah; /*** * * @author team * */ public class Demo1 { public static void main(String[] args) { /*** * 反斜線\在程序中必須以\\表示以下: */ System.out.println("\\".matches("\\\\")); /*** * ("-?\\d+")匹配:可能有一個負號,或者後面跟着一位或多位數字 */ System.out.println("-1234".matches("-?\\d+")); System.out.println("5678".matches("-?\\d+")); System.out.println("+911".matches("-?\\d+")); /*** * ("(-|\\+)?\\d+")匹配:表示字符串的起始字符多是一個-或者+(+有特殊意義,須要用\\轉義),後面跟着一位或多位數字 */ System.out.println("+911".matches("(-|\\+)?\\d+")); } }
運行結果:工具
true true true false true
Demo2
String.spilt是一個很是有用的正則表達式工具,其功能是」將字符串從正則表達式匹配的地方切開「。spa
package com.rah; import java.util.Arrays; /*** * * @author team * */ public class Demo2 { public static String knights = "Then, when you have found the shrubbery, you must " + "cut down the mightiest tree in the forest..." + "with... a herring!"; public static void split(String regex) { System.out.println(Arrays.toString(knights.split(regex))); } public static void main(String[] args) { /*** * 按空格劃分字符串 */ split(" "); /*** * \W(\\W轉義)意思是非單詞字符若是是小寫W,\w則表示一個單詞字符 * 該正則能夠標點字符給刪了 */ split("\\W+"); /*** * 字母n後面跟着一個單詞字符 */ split("n\\W+"); } }
運行結果:.net
[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest...with..., a, herring!]
[Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring]
[The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest...with... a herring!]
Demo3rest
String.replaceFirst()/replaceAll(),也是能夠匹配正則的code
package com.rah; /*** * * @author team * */ public class Demo3 { public static String sqlOne = "select * from students"; public static String sqlTwo = "seelct count(*) from students"; public static void main(String[] args) { /*** * 從前四個輸出能夠看出[]裏面是隻匹配他就會在第一時間匹配到就不會往下找,就是由於這個小知識點,在開發中浪費了我好多時間Q_Q */ System.out.println(sqlOne.replaceFirst("s", "count(*)")); System.out.println(sqlOne.replaceFirst("[s]", "count(*)")); /*** * 找到se匹配 */ System.out.println(sqlOne.replaceFirst("se", "count(*)")); /*** * 找到s匹配,就不會往下找 */ System.out.println(sqlOne.replaceFirst("[se]", "count(*)")); System.out.println(sqlOne.replaceFirst("[*]", "count(*)")); System.out.println(sqlTwo.replaceFirst("count\\(\\*\\)", "*")); } }
運行結果:
count(*)elect * from students count(*)elect * from students count(*)lect * from students count(*)elect * from students select count(*) from students seelct * from students
Demo4:
檢查句子以大寫字母開頭、以句號結尾
package com.rah; /*** * * @author team * */ public class Demo4 { public static boolean matches(String text) { /*** * \\p{javaUpperCase} 大寫字母,不明白的能夠看jdk文檔 */ return text.matches("\\p{javaUpperCase}.*\\."); } public static void main(String[] args) { System.out.println(matches("This is correct.")); System.out.println(matches("bad sentence 1.")); System.out.println(matches("Bad sentence 2")); System.out.println(matches("This is also correct...")); } }
運行結果:
true false false true
Demo5:
package com.rah; import java.util.Arrays; /*** * * @author team * */ public class Demo5 { public static String knights = "Then, when you have found the shrubbery, you must " + "cut down the mightiest tree in the forest..." + "with... a herring!"; public static void split(String regex) { System.out.println(Arrays.toString(knights.split(regex))); } public static void main(String[] args) { /*** * 在the和you處分割 */ split("the|you"); } }
運行結果:
[Then, when , have found , shrubbery, , must cut down , mightiest tree in , forest...with... a herring!]
Demo6
package com.rah; /*** * * @author team * */ public class Demo5 { public static String knights = "Then, when you have found the shrubbery, you must " + "cut down the mightiest tree in the forest..." + "with... a herring!"; /* * 對應的內嵌標誌表達式是 (?i),它有四種形式: * 1,(?i) * 2,(?-i) * 3,(?i:X) * 4,(?-i:X) * 不帶有 - 的是開標誌,帶有 - 的是關標誌。 */ public static void main(String[] args) { /*** * 對book都忽略大寫 */ System.out.println("Book".matches("(?i)Book")); /*** * 對b都忽略大寫,ook仍是得比較大小寫,下面的方法做用同樣,寫的更簡潔 */ System.out.println("Book".matches("(?i)b(?-i)ook")); /*** * 對b都忽略大寫,ook仍是得比較大小寫 */ System.out.println("Book".matches("(?i:b)ook")); /*** * (?-i) 的做用域是前面,如a(?-i) (?-i)的做用域是後面,如(?i)B */ System.out.println("bOOk".matches("b(?-i)(?i)ook")); System.out.println("aBook".matches("a(?-i:B)ook")); /*** * [] 只要匹配到一個再往下匹配,匹配不到了就把當前的替換 * 沒有[] 他要知足字符串到匹配到才換 */ System.out.println("ouahoahuah".replaceAll("[ou]", "")); System.out.println("ouahoahuah".replaceAll("ou", "")); /*** * 忽略大小寫匹配aeiou */ System.out.println(knights.replaceAll("(?i)[aeiou]", "")); } }
運行結果:
true true true true true ahahah ahoahuah Thn, whn y hv fnd th shrbbry, y mst ct dwn th mghtst tr n th frst...wth... hrrng!
2、建立正則表達式
寫法參考java.util.regex包下的Pattern類
Demo7:
package com.rah; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo7 { public static void main(String[] args) { if(args.length<2){ System.out.println("Usage:\njava TestRegularExpression " + "characterSequence regularExpression"); System.exit(0); } System.out.println("Input: \"" + args[0] + "\""); for(String arg : args){ System.out.println("Regulqr expression: \"" +arg +"\""); Pattern p = Pattern.compile(arg); Matcher m = p.matcher(args[0]); while(m.find()){ System.out.println("Match \"" + m.group() + "\" at position " + m.start() + "-" + (m.end()-1)); } } } }
傳入的參數:
abcabcabcdefabc abc+ (abc)+ (abc){2,}
運行結果:
Input: "abcabcabcdefabc" Regulqr expression: "abcabcabcdefabc" Match "abcabcabcdefabc" at position 0-14 Regulqr expression: "abc+" Match "abc" at position 0-2 Match "abc" at position 3-5 Match "abc" at position 6-8 Match "abc" at position 12-14 Regulqr expression: "(abc)+" Match "abcabcabc" at position 0-8 Match "abc" at position 12-14 Regulqr expression: "(abc){2,}" Match "abcabcabc" at position 0-8
Demo8:
package com.rah; import java.util.regex.Matcher; import java.util.regex.Pattern; /*** * * @author team * */ public class Demo8 { public static void main(String[] args) { Matcher m = Pattern.compile("\\w+").matcher("Evening is full of the linnet's wings"); while(m.find()) System.out.print(m.group() + " "); System.out.println(); int i = 0; /*** * find(args) 是字符的起始位置,注意輸出結果 */ while(m.find(i)) { System.out.print(m.group() + " "); i++; } } }
運行結果:
Evening is full of the linnet s wings
Evening vening ening ning ing ng g is is s full full ull ll l of of f the the he e linnet linnet innet nnet net et t s s wings wings ings ngs gs s
Demo9 (Group組)
組是用括號提供劃分的正則表達式,能夠根據組的編號來引用某個組。組號爲0表示整個表達式組號爲1表示被第一對括號括起來的組。
a(b(c))d abcd是組0 bc是組1 c是組2
package com.rah; import java.util.regex.Matcher; import java.util.regex.Pattern; /*** * * @author team * */ public class Demo9 { static public final String POEM = "Twas brillig, and the slithy toves\n" + "Did gyre and gimble in the wabe.\n" + "All mimsy were raths outgrabe.\n" + "And the mome raths outgrabe.\n\n" + "Beware the Jabberwock, my son,\n" + "The jaws that bite, the claws that catch.\n" + "Beware the Jubjub bird, and shun\n" + "The frumious Bandersnatch."; public static void main(String[] args) { /*** * 檢索每行的3個單詞,每行最後以$結尾。一般$是與整個輸入序列的末端進行匹配的,爲了達到每行最後以$結尾,咱們須要顯示的通知正則表達式注意輸入 * 序列中的換行符,這個工做就由模式標記(?m)來完成。 */ Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(POEM); while(m.find()) { for (int i = 0; i <= m.groupCount(); i++) { System.out.print("[" + m.group(i) + "]" + " "); } System.out.println(); } } }
運行結果:
[the slithy toves] [the] [slithy toves] [slithy] [toves] [in the wabe.] [in] [the wabe.] [the] [wabe.] [were raths outgrabe.] [were] [raths outgrabe.] [raths] [outgrabe.] [mome raths outgrabe.] [mome] [raths outgrabe.] [raths] [outgrabe.] [Jabberwock, my son,] [Jabberwock,] [my son,] [my] [son,] [claws that catch.] [claws] [that catch.] [that] [catch.] [bird, and shun] [bird,] [and shun] [and] [shun] [The frumious Bandersnatch.] [The] [frumious Bandersnatch.] [frumious] [Bandersnatch.]
Demo10
?:、?!、?s、?i、?x、?m、?u、?d等的使用
未完待續。。。