Regex
- Regex(Regular Expression,正则表达式):描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
1. 概念及演示
- 在 Java 中,我们经常需要验证一些字符串,例如:年龄必须是 2 位的数字、用户名必须是 8 位长度而且只能包含大小写字母、数字等。正则表达式就是用来验证各种字符串的规则。它内部描述了一些规则,我们可以验证用户输入的字符串是否匹配这个规则。
- 先看一个不使用正则表达式验证的例子:下面的程序让用户输入一个 QQ 号码,我们要验证:
- QQ 号码必须是 5 - 15 位长度;
- 而且必须全部是数字;
- 而且首位不能为 0。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的QQ号码:");
String qq = sc.next();
System.out.println(checkQQ(qq));
}
private static boolean checkQQ(String qq) {
if (qq.length() < 5 || qq.length() > 15) {
return false;
}
for (int i = 0; i < qq.length(); i++) {
char c = qq.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
char c = qq.charAt(0);
if (c == '0') {
return false;
}
return true;
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的QQ号码:");
String qq = sc.next();
System.out.println(checkQQ2(qq));
}
private static boolean checkQQ2(String qq) {
String regex = "[1-9]\\d{4,14}";
return qq.matches(regex);
}
}
- 上面程序 checkQQ2() 方法中 String 类型的变量 regex 就存储了一个 "正则表达式 ",而这个正则表达式就描述了我们需要的三个规则。matches() 方法是 String 类的一个方法,用于接收一个正则表达式,并将 “本对象” 与参数 “正则表达式” 进行匹配,如果本对象符合正则表达式的规则,则返回 true,否则返回 false。
- 那么如何写出正则表达式呢?
2. 字符类
- 语法示例:
- [abc]:代表 a 或者 b,或者 c 字符中的一个;
- [^abc]:代表除 a,b,c 以外的任何字符;
- [a-z]:代表 a-z 的所有小写字符中的一个;
- [A-Z]:代表 A-Z 的所有大写字符中的一个;
- [0-9]:代表 0-9 之间的某一个数字字符;
- [a-zA-Z0-9]:代表 a-z 或者 A-Z 或者 0-9 之间的任意一个字符;
- [a-dm-p]:a 到 d 或 m 到 p 之间的任意一个字符。
- 代码示例:
public class Test {
public static void main(String[] args) {
String str = "ead";
String regex = "h[aeiou]d";
System.out.println("1." + str.matches(regex));
regex = "h[^aeiou]d";
System.out.println("2." + str.matches(regex));
regex = "[a-z]ad";
System.out.println("3." + str.matches(regex));
regex = "[[a-d][m-p]]ad";
System.out.println("4." + str.matches(regex));
}
}
3. 逻辑运算符
- 语法示例:
- &&:并且;
- | :或者;
- 代码示例:
public class Test {
public static void main(String[] args) {
String str = "had";
String regex = "[a-z&&[^aeiou]]ad";
System.out.println("1." + str.matches(regex));
regex = "[a|e|i|o|u]ad";
System.out.println("2." + str.matches(regex));
}
}
4. 预定义字符
- 语法示例:
- “.” : 匹配任何字符;
- “\d”:任何数字 [0-9] 的简写;
- “\D”:任何非数字 [^0-9] 的简写;
- “\s”: 空白字符 [ \t\n\x0B\f\r] 的简写;
- “\S”: 非空白字符 [^\s] 的简写;
- “\w”:单词字符 [a-zA-Z_0-9] 的简写;
- “\W”:非单词字符 [^\w]。
- 代码示例:
public class Test {
public static void main(String[] args) {
String str = "258";
String regex = "\\d\\d\\d";
System.out.println("1." + str.matches(regex));
str = "13513153355";
regex = "1[358]\\d\\d\\d\\d\\d\\d\\d\\d\\d";
System.out.println("2." + str.matches(regex));
str = "had";
regex = "h.d";
System.out.println("3." + str.matches(regex));
str = "had.";
regex = "had\\.";
System.out.println("4." + str.matches(regex));
}
}
5. 数量词
- 语法示例:
- X? : 0 次或 1 次;
- X* : 0 次到多次;
- X+ : 1 次或多次;
- X{n} : 恰好 n 次;
- X{n,} : 至少 n 次;
- X{n,m}: n 到 m 次(n 和 m 都是包含的)
- 代码示例:
public class Test {
public static void main(String[] args) {
String str = "";
str = "012";
String regex = "\\d{3}";
System.out.println("1." + str.matches(regex));
str = "88932054782342";
regex = "\\d+";
System.out.println("2." + str.matches(regex));
str = "13813183388";
regex = "1[358]\\d{9}";
System.out.println("3." + str.matches(regex));
String s2 = "3.1";
regex = "\\d*\\.{1}\\d+";
System.out.println("4." + s2.matches(regex));
regex = "\\d+\\.?\\d+";
System.out.println("5." + s2.matches(regex));
s2 = "-3.";
regex = "[+-]\\d+\\.?\\d*";
System.out.println("6." + s2.matches(regex));
s2 = "1695827736";
regex = "[1-9]\\d{4,14}";
System.out.println("7." + s2.matches(regex));
}
}
6. 分组括号( )
public class Test {
public static void main(String[] args) {
String str = "DG8FV-B9TKY-FRT9J-99899-XPQ4G";
String regex = "([A-Z0-9]{5}-){4}[A-Z0-9]{5}";
System.out.println(str.matches(regex));
}
}
7. String 的 split 方法中使用正则表达式
public String[] split(String regex)
public class Test {
public static void main(String[] args) {
String str = "18 4 567 99 56";
String[] strArray = str.split(" +");
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
}
}
8. String 类的 replaceAll 方法中使用正则表达式
- String 类的 replaceAll() 方法原型:
public String replaceAll(String regex,String newStr)
public class Test {
public static void main(String[] args) {
String str = "jfdk432jfdk2jk24354j47jk5l31324";
System.out.println(str.replaceAll("\\d+", "*"));
}
}