正則表達式: 是指一個用來描述或者匹配一系列符合某個句法規則的字符串的單個字符串。其實就是一種規則,有自己特殊的應用。
常用的如下:
字符類:
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 * \d
15 * "\\"代表一個"\"
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 * \b
26 */
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 //匹配校驗郵箱的正則表達式
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.根據正則表達式創建模式Pattern
10 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 }