正则表达式用于对字符串的匹配判断中


元字符:

元字符 正则表达式中的写法 意义
. . 代表任意一个字符
\d \\d 代表0 - 9任意一个数字字符
\D \\D 代表任意一个非数字字符
\s \\s 代表空白字符, 如’\n’ , ‘\t’
\S \S 代表分空白字符
\w \w 代表可用作标识符的字符, 但不包括 “$”
\W \W 代表不可以用作标识符的字符
\p{Lower} \p{Lower} 代表小写字母 a ~ z
\p{Upper} \p{Upper} 代表大写字母 A ~ Z
\p{ASCII} \p{ASCII} ASCII 字符
\p{Alpha} \p{Alpha} 字母字符
\p{Digit} \p{Digit} 十进制数字
\p{Alnum} \p{Alnum} 数字或字母字符
\p{Punct} \p{Punct} 标点符号 : “!@#$%^&、*()[]“等等
\p{Graph} \p{Graph} 可见字符: [\p{Alnum}\p{Punct}]
\p{Print} \p{Print} 可打印字符: [\p{Graph}\x20]
\p{Blank} \p{Blank} 空格或者制表符: [\t]
\p{Cntrl} \p{Cntrl} 控制字符: [\x00-\x1F\x7F]

在正则表达式中 “ . “ 代表任意一个字符,所以在使用时候如果要判断有普通意义的”.”需要加斜杆(和Latex特殊字符控制一样)

  • 例子:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;

    public class RegexExample {
    public static void main(String[] args) {
    String regex = "\\d+"; // 匹配一个或多个数字
    String input = "12345";

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);

    if (matcher.matches()) {
    System.out.println("完全匹配!");
    } else {
    System.out.println("不匹配!");
    }
    }
    }

事实上,String类里面封装了Pattern类,也可以通过一下方式来调用Pattern里面的匹配:

1
input.matches(regex) //其内部调用了 `Pattern.matches(regex, this)`


元字符与基本符号(!important)

  • .
    匹配除换行符外的任意单个字符。例如:a.c 可以匹配 “abc”、”a-c” 等。

匹配前面的元素 0 次或多次。例如:`ab*c` 可以匹配 "ac"、"abc"、"abbc" 等。
  • +
    匹配前面的元素 1 次或多次。例如:ab+c 不能匹配 “ac”,但可以匹配 “abc”、”abbc” 等。

  • ?
    匹配前面的元素 0 次或 1 次。例如:colou?r 可以匹配 “color” 和 “colour”。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    String regex = "abcdd?e"; // 匹配一个或零个数字  
    String input = "abcde";

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);

    if (matcher.matches()) {
    System.out.println("完全匹配!");
    } else {
    System.out.println("不匹配!");
    }
    // 结果是匹配,因为该位置的d没有出现(0次),其他基本符号用法同理
  • {}
    限定匹配次数:

    • {n}:匹配恰好 n 次
    • {n,}:至少 n 次
    • {n,m}:至少 n 次,最多 m 次
      例如:\\d{3} 匹配连续3个数字。
  • []
    定义字符集合,例如:[abc] 匹配 ‘a’、’b’ 或 ‘c’;[a-z] 匹配所有小写字母。

  • ^$
    分别表示字符串的开始和结束。例如:^Hello 表示字符串必须以 “Hello” 开始,world$ 表示必须以 “world” 结尾。

  • |
    表示“或”的关系。例如:cat|dog 表示匹配 “cat” 或 “dog”


多个元字符代表一个字符(字符集合)

在正则表达式中可以使用方括号括起若干字符表示一个元字符,该字符可以表示元字符中的任意一个字符。例如: reg = “[abc]4”, 这样a4, b4, c4都是和正则表达式匹配的字符串。方括号还可以为其他格式。如:

  • [^456]除了456之外的任何字符
  • [a-r]代表a - r中的任何一个字符
  • [a-zA-Z]: 代表任何一个英文字母
  • [a-e[g-z]]: 代表a - e g - z 间的字母
  • [a-o&&[def]]: 代表字母def, 并运算
  • [a-d&&[^bc]]: 代表a, d(差运算)

实际例子

一. 判断字符串是否是邮箱:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Matcher;  
import java.util.regex.Pattern;
import java.util.Scanner;

public class Javastudy {
public static void main(String[] args) { // 验证字符串是否是一个邮箱地址
String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
String input = "511017802@qq.com";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
Scanner in = new Scanner(System.in);

String s = in.nextLine(); // s = input;

if (s.matches(regex)) {
System.out.println("完全匹配!");
} else {
System.out.println("不匹配!");
}
in.close();
}
}

二. 判断字符串是否是电话号码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.regex.Matcher;  
import java.util.regex.Pattern;
import java.util.Scanner;

public class Javastudy {
public static void main(String[] args) { // 验证字符串是否是一个邮箱地址
String regex = "^(\\+?86)?1[3-9]\\d{9}$";

Scanner in = new Scanner(System.in);

int n;
n = in.nextInt();
in.nextLine();

while (n-- != 0) {
String s = in.nextLine();

if (s.matches(regex)) {
System.out.println("是中国大陆电话号码");
} else {
System.out.println("乱搞");
}
}
}
}

三:判断字符串是否是网址:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.regex.Matcher;  
import java.util.regex.Pattern;
import java.util.Scanner;

public class Javastudy {
public static void main(String[] args) { // 验证字符串是否是一个邮箱地址
String regex = "^(https|http)*w{3}\\.\\w+\\.\\w{2,3}";

Scanner in = new Scanner(System.in);

String s = "www.dreamstartooo.cn";

if (s.matches(regex)) {
System.out.println(s + "是一个好网站");
} else {
System.out.println("乱搞");
}
}}
//输出 www.dreamstartooo.cn 是一个好网站!