Regex,正則
Regex: 是指一個用來描述或者匹配一系列符合某個句法規則的字串的單個字串。其實就是一種規則,有自己特殊的應用。
常用的如下:
字元類:
x x表示由一個字元所組成 \ \\表示逸出字元“\” \t \t(定位字元)表示一個“\t”符號 \n \n表示分行符號號 [abc] a或b或c [^abc] 除了a、b、c之外的任一字元 【a-z】 a-z中的任一字元,小寫字母; [a-zA-Z] a-zA-Z中的任一字元,不區分大小寫 [0-9] 表示任意的一位元字
預定義字元類: . 任何一位字元 \d 數字:[0-9] \D 非數字:[^0-9] \w 單詞字元:[a-zA-Z_0-9] \W 非單詞字元:[^\w] \s 表示任一的空白字元,例如:\t \n \S 表示任一的非空白字元
邊界匹配器(主要在js中使用): ^ 行的開頭 $ 行的結尾 \b 單詞邊界例: "I am here".matches("I\\b \\bam\\b \\bhere") 注意:邊界所處的位置。
Greedy--數量運算式 X? X,一次或一次也沒有 正則? 表示正則出現一次或者零次; X* X,零次、一次或多次 X+ X,一次或多次 X{n} X,恰好 n 次 X{n,m} X,至少 n 次,但是不超過 m 次
程式碼範例:
1 package com.ibeifeng.regex; 2 public class RegexDemo2 { 3 public static void main(String[] args) { 4 /* 5 * [abc] a或b或c 6 * [^abc] 除了a、b、c之外的任一字元 7 * [a-zA-Z] a-zA-Z中的任一字元 8 */ 9 System.out.println("d".matches("[abc]"));10 System.out.println("a".matches("[^abc]"));11 System.out.println("aa".matches("[a-zA-Z]"));12 13 /*14 * \d15 * "\\"代表一個"\"16 * \w 0-9a-zA-Z_17 */18 System.out.println("a".matches("\\d"));19 System.out.println("~".matches("\\D"));20 System.out.println("0".matches("\\w"));21 22 /*23 * ^24 * $25 * \b26 */27 System.out.println("acbc".matches("^a\\w\\w\\w"));28 System.out.println("abcde".matches("\\w\\w\\w\\we$"));29 30 /*31 * ? 1次或0次32 * * 0次或多次33 * + 1次或多次34 * {n} 恰好N次35 * {m,n} 至少m次 至多n次36 */37 System.out.println("carsdff".matches("\\w*"));38 System.out.println("teacher".matches("\\w+"));39 System.out.println("abcq".matches("\\w{3}"));40 System.out.println("===================");41 System.out.println("I am here".matches("I\\b \\bam\\b \\bhere"));42 System.out.println("a".matches("."));43 }44 }
程式碼範例(/檢測郵箱的功能):
1 package com.ibeifeng.regex; 2 public class RegexDemo3 { 3 public static void main(String[] args) { 4 //匹配校正郵箱的Regex 5 String s1 = "abcde@163.com"; 6 String s2 = "abcdefg@qq.com"; 7 String s3 = "adfdfw@sina.com.cn"; 8 String s4 = "fwef#com.cn.cn.cn"; 9 /*10 * . \.11 * @ \@12 */13 System.out.println(s4.matches("\\w+\\@\\w+(\\.\\w{2,3}){1,2}"));14 }15 }
分割功能:
範例:嘗試輸入一個年齡 顯示是否是在範圍內的合適年齡 提示大小
1 package com.ibeifeng.regex; 2 import java.util.Arrays; 3 import java.util.Scanner; 4 /* 5 * public String[] split(String regex) 6 */ 7 public class RegexDemo5 { 8 public static void main(String[] args) { 9 //String age = “18-28”;10 //嘗試輸入一個年齡 顯示是否是在範圍內的合適年齡 提示大小11 String age = "18-28";12 String[] split = age.split("-");13 System.out.println(Arrays.toString(split));14 int min = Integer.valueOf(split[0]);15 int max = Integer.valueOf(split[1]);16 Scanner scanner = new Scanner(System.in);17 System.out.print("請輸入你需要的年齡的對象:");18 int nextInt = scanner.nextInt();19 if(nextInt > max){20 System.out.println("哈哈");21 }else if (nextInt < min) {22 System.out.println("兒童哦");23 }else {24 System.out.println("這裡就有你想要的.");25 }26 }27 }
替換功能:
1 package com.ibeifeng.regex; 2 import java.util.Arrays; 3 public class RegexDemo6 { 4 public static void main(String[] args) { 5 6 7 /* 8 * public String replaceAll(String regex, 9 * String replacement)10 */11 //fuck f*** shit s*** cao c**12 String str5 = "Oh shit! cao! what a fucking weather, what are you fucking doing!";13 String replaceAll = str5.replaceAll("fuck", "f***")14 .replaceAll("shit", "s***")15 .replaceAll("cao", "c**");16 17 System.out.println(replaceAll);18 19 String str6 = "show me the money 999999";20 //數字替換掉 同一替換成*21 System.out.println(str6.replaceAll("\\d+", "*"));22 System.out.println(str6.replaceAll("\\d{3}", "*"));23 }24 }
擷取功能:
Pattern和Matcher類的使用Pattern類:此類對象如果要想取得必須使用compile()方法,方法的功能是編譯正則;Matcher類:是通過Pattern類取得。步驟: 1.通過編譯規則得到模式Patterm; 2.通過模式比對字元序列得到匹配器; 3.通過匹配器得到find,在group裡擷取。 Pattern pattern = Pattern.compile(String regex); Matcher matcher = pattern.matcher(String str); while(matcher.find()){ String str = matcher.group(); }
1 package com.ibeifeng.regex; 2 import java.util.regex.Matcher; 3 import java.util.regex.Pattern; 4 public class PatternDemo { 5 public static void main(String[] args) { 6 //擷取由三個字元組成的單詞 7 //You may be out of my sight, but never out of my mind. 8 String s = "You may be out of my sight, but never out of my mind"; 9 //1.根據Regex建立模式Pattern10 Pattern pattern = Pattern.compile("\\b\\w{3}\\b");11 //2.通過模式比對字元序列得到匹配器12 Matcher matcher = pattern.matcher(s);13 //3.通過匹配器find,在group裡擷取14 int num = 0;15 while(matcher.find()){16 String group = matcher.group();17 int start = matcher.start();18 int end = matcher.end();19 System.out.println(group);20 System.out.println(start+"==="+end);21 num ++;22 }23 System.out.println(num+"次");24 25 /*matcher.find();26 String group = matcher.group();27 System.out.println(group);28 29 matcher.find();30 group = matcher.group();31 System.out.println(group);*/32 }33 }