案例1:判斷字符串是否是abc
package Regex; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 1.正則對應的類是Pattern(翻譯:模式)而不是Rxgex,我們用到的方法都在Pattern類里面 * 2.案例1:判斷字符串是否是abc */ public class PatternDemo01 { public static void main(String[] args) { String str = "abc"; // 根據以前的方法進行判斷 // System.out.println(str.equals("abc")); /** * 用正則表達式來判斷 * 1.compile(String regex) 將給定的正則表達式編譯到模式中。 * 2.matcher(CharSequence input) 創建匹配給定輸入與此模式的匹配器。 * 3.matches() 嘗試將整個區域與模式匹配。 */ // 首先要編譯正則規則形式 Pattern p = Pattern.compile("abc"); // 將正則進行匹配 Matcher m = p.matcher(str); // 進行判斷 boolean b = m.matches(); System.out.println(b); } }
案例2:判斷一個字符串
package Regex; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 案例2:判斷一個字符串 * 由3個字母組成 * 第一個字母是 a/b/c * 第二個字母是 d/e/f/g * 第三個字母是 x/y/z */ public class PatternDemo02 { public static void main(String[] args) { String str = "agx"; // 指定判斷規則(中括號內字符順序隨便) Pattern p = Pattern.compile("[abc][edgf][xzy]"); // 進行規則匹配 Matcher m = p.matcher(str); // 進行判斷 boolean b = m.matches(); System.out.println(b); } }
案例3:判斷一個字符串
package Regex; /** * 案例3:判斷一個字符串 * 由3個字母組成 * 第一個字母是 a/b/c * 第二個字母是 d/e/f/g * 第三個字母是 x/y/z */ public class PatternDemo03 { public static void main(String[] args) { String str = "agx"; // 將原來的3個步驟封裝到一步進行判斷 System.out.println(str.matches("[abc][defg][zyx]")); } }
案例4:字符類
[abc] a、b 或 c(簡單類)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,兩頭的字母包括在內(范圍)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](並集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](減去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](減去)
package Regex; /** * [abc] a、b 或 c(簡單類) * [^abc] 任何字符,除了 a、b 或 c(否定) * [a-zA-Z] a 到 z 或 A 到 Z,兩頭的字母包括在內(范圍) */ public class PatternDemo04 { public static void main(String[] args) { // 判斷由一個字母組成的字符串 String str1 = "D"; System.out.println(str1.matches("[a-zA-Z]")); // 判斷由一個數字組成的字符串 String num = "2"; System.out.println(num.matches("[0-9]")); // 判斷由一個字符組成的字符串,但不是 a/b/c String str2 = "/"; System.out.println(str2.matches("[^a-c]")); } }
案例5:預定義字符類
package Regex; /** * 案例5:預定義字符類 * . 任何一個字符(與行結束符可能匹配也可能不匹配) */ public class PatternDemo05 { public static void main(String[] args) { // 判斷一個由a開頭的由兩個字符組成的字符串 String str1 = "af"; System.out.println(str1.matches("a.")); // 判斷是否是一個 . // Java 用\\進行轉義 String str2 = "."; System.out.println(str2.matches("\\.")); // 匹配一個 \ String str3 = "\\"; System.out.println(str3.matches("\\\\")); } }
案例6:判斷小數
package Regex; /** * 案例6:Greedy 數量詞 * X+ 代表 X至少出現一次或多次 >=1 * * Reluctant 數量詞 * X* 代表X出現零次或多次 >=0 * * X? 表示X至多出現一次 */ public class PatternDemo06 { public static void main(String[] args) { // 判斷由a開頭,至少有三個字符組成的字符串 String str1 = "asdfuj312s"; System.out.println(str1.matches("a.+")); // 判斷由小寫字母開頭,數字結尾的字符串 String str2 = "a3"; System.out.println(str2.matches("[a-z].*[0-9]")); // 判斷由a開頭至多有2個字符組成的字符串 String str3 = "a"; System.out.println(str3.matches("a.?")); // 判斷小數 String str4 = "0.4"; System.out.println(str4.matches("[0-9]+\\.\\d+")); System.out.println(str4.matches("0\\.\\d+") || str4.matches("[1-9]\\d*\\.\\d+")); } }
案例7:密碼校驗:8-20位,小寫字母/大寫字母/數字中的至少兩種
package Regex; /** * 案例7: * X{n} X,恰好 n 次 * X{n,} X,至少 n 次 * X{n,m} X,至少 n 次,但是不超過 m 次 */ public class PatternDemo07 { public static void main(String[] args) { // 判斷由5個小寫字母組成的字符串 String str1 = "gjhad"; System.out.println(str1.matches("[a-z]{5}")); // 判斷由至少5個小寫字母組成的字符串 String str2 = "sdfguhvujhsdfkgvjhsdf"; System.out.println(str2.matches("[a-z]{5,}")); // 判斷由8-12個小寫字母組成的字符串 String str3 = "weqrtyuio"; System.out.println(str3.matches("[a-z]{8,12}")); // 練習:密碼校驗:8-20位,小寫字母/大寫字母/數字中的至少兩種 String str4 = "qwe123SAD"; check(str4); } public static boolean check(String str) { // 判斷輸入的密碼是否出現規定以外的字符 if(!str.matches("[a-zA-Z0-9]{8,20}")) return false; // 記錄出現幾種字符 int count = 0; // 如果出現小寫字母 if(str.matches(".*[a-z].*")) count++; // 如果出現大寫字母 if(str.matches(".*[A-Z].*")) count++; // 如果出現數字 if(str.matches(".*[0-9].*")) // str.matches(".*\\d.*") count++; // 如果count < 2 if(count < 2) { System.out.println("密碼錯誤!"); return false; }else return true; } }
案例8:()捕獲組
package Regex; /** * 案例8: * 1.()捕獲組 * 2.正則表達式會對其中的捕獲組進行自動編號 * 3.從1開始編號 * 4.\\n 表示引用前邊編號為n的捕獲組 * */ public class PatternDemo08 { public static void main(String[] args) { /** * 捕獲組的編號是從捕獲組的左半邊括號出現的位置開始計算的 * (A((BC)(D))E)(F) * 1號:A((BC)(D))E * 2號:(BC)(D) * 3號:BC * 4號:D * 5號:F */ String str1 = "abfdabasdf"; System.out.println(str1.matches(".*(ab).*\\1.*")); // 匹配至少由兩個字符組成的字符串 System.out.println(str1.matches("(.){2,}")); } }
案例9:運用捕獲組判斷疊字
package Regex; /** * 案例9: * 運用捕獲組判斷疊字 */ public class PatternDemo09 { public static void main(String[] args) { // 匹配AAA形式的疊字字符串 String str1 = "aaa"; System.out.println(str1.matches("(.)\\1+")); // 匹配AABB形式的疊字字符串 String str2 = "AABB"; System.out.println(str2.matches("(.)\\1(.)\\2")); // 匹配ABAB形式的疊字字符串 String str3 = "ABAB"; System.out.println(str3.matches("(..)\\1")); } }
案例10:郵箱格式校驗
package Regex; /** * 案例10:郵箱格式校驗 * */ public class PatternDemo10 { public static void main(String[] args) { String str1 = "42513@qq.com"; String str2 = "cjjsdfasf@sina.com.cn"; String str3 = "24eqwe"; check(str1); check(str2); check(str3); } public static void check(String str) { // 以.cn結尾的郵箱 if(str.matches("[a-zA-Z0-9]{6,36}@[a-zA-Z0-9]{3,10}(\\.com)?(\\.cn)") ||str.matches("[a-zA-Z0-9]{3,10}@[a-zA-Z0-9]{1,36}(\\.com)")) { System.out.println("ok"); }else System.out.println("no"); } }
案例11:利用正則表達式替換字符串內指定字符
package Regex; /** * 利用正則表達式替換字符串內指定字符 * */ public class PatternDemo11 { public static void main(String[] args) { String str = "261refdae612c,./d/"; // 將字符串內所有數字替換為- System.out.println(str.replaceAll("\\d", "-")); // 將字符串內所有非數字去掉 System.out.println(str.replaceAll("\\D", "")); } }
案例12:輸入一個字符串,統計字符串中每一個字符出現的次數
package Regex; /** * 輸入一個字符串,統計字符串中每一個字符出現的次數 * */ public class PatternDemo12 { public static void main(String[] args) { String str = "sdafv187623rtajhsd"; fin(str); } public static void fin(String str) { // 獲取第一個字符 char c = str.charAt(0); // 將第一個字符都替換為空 String str1 = str.replaceAll("" + c, ""); // 計算兩個字符串的長度 int len = str.length() - str1.length(); System.out.println(str.charAt(0) + ":" + len); // 如果替換后的字符串長度大於0.說明還沒有統計完,再次調用統計方法 if(str1.length() > 0) fin(str1); } }
案例13:交換字符串中指定位置的字符
package Regex; /** * 交換字符串中指定位置的字符 * */ public class PatternDemo13 { public static void main(String[] args) { String str = "I Like Beijing."; System.out.println(str.replaceAll("(I )(Like )(Beijing.)", "$3$2$1")); } }
案例14:將疊字替換為單字
package Regex; /** * 將疊字替換為單字 */ public class PatternDemo14 { public static void main(String[] args) { String str = "AAAAABBBBBDDDDDGGGGG"; System.out.println(str.replaceAll("(.)\\1+", "$1")); } }
案例15:計算字符串的平均長度
package Regex; /** * 計算字符串的平均長度 * */ public class PatternDemo15 { public static void main(String[] args) { String str = "aaabbbdjhjjjjkkkkk(((jjjooo"; fin(str); } public static void fin(String str) { // 字符串長度 double len = str.length(); // 將疊字換成單字 String str1 = str.replaceAll("(.)\\1+", "a"); // 統計單字組成的長度 double len1 = str1.length(); double n = len / len1; System.out.println(n); } }
案例16:以數字作為切割符將字符切開
package Regex; import java.util.Arrays; /** * 以數字作為切割符將字符切開 * 作為切割符的符號會被切掉 * 在字符串最尾部的切割符會整個切掉 */ public class PatternDemo16 { public static void main(String[] args) { String str = "2iasug8jhfch9sjba90"; /** * 1.split(String regex) * 根據給定正則表達式的匹配拆分此字符串。 * 2.toString(Object[] a) * 返回指定數組內容的字符串表示形式。 */ // str1是字符串數組 String[] str1 = str.split("\\d"); // 將字符串數組抓換成字符串 System.out.println(Arrays.toString(str1)); } }