这篇文章主要为大家详细介绍了java的正则表达式,使用表格进行介绍,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
| 字符 |
|---|
| x | 字符 x |
| \\ | 反斜线字符 |
| \0n | 带有八进制值 0 的字符 n (0 <= n <= 7) |
| \0nn | 带有八进制值 0 的字符 nn (0 <= n <= 7) |
| \0mnn | 带有八进制值 0 的字符 mnn(0 <= m <= 3、0 <= n <= 7) |
| \xhh | 带有十六进制值 0x 的字符 hh |
| \uhhhh | 带有十六进制值 0x 的字符 hhhh |
| \t | 制表符 ('\u0009') |
| \n | 新行(换行)符 ('\u000A') |
| \r | 回车符 ('\u000D') |
| \f | 换页符 ('\u000C') |
| \a | 报警 (bell) 符 ('\u0007') |
| \e | 转义符 ('\u001B') |
| \cx | 对应于 x 的控制符 |
| 字符类 |
|---|
| [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](减去) |
| 预定义字符类 |
|---|
| . | 任何字符(与行结束符可能匹配也可能不匹配) |
| \d | 数字:[0-9] |
| \D | 非数字: [^0-9] |
| \s | 空白字符:[ \t\n\x0B\f\r] |
| \S | 非空白字符:[^\s] |
| \w | 单词字符:[a-zA-Z_0-9] |
| \W | 非单词字符:[^\w] |
| Greedy 数量词 |
|---|
| X? | X,一次或一次也没有 |
| X* | X,零次或多次 |
| X+ | X,一次或多次 |
| X{n} | X,恰好 n 次 |
| X{n,} | X,至少 n 次 |
| X{n,m} | X,至少 n 次,但是不超过 m 次 |
| Reluctant 数量词 |
|---|
| X?? | X,一次或一次也没有 |
| X*? | X,零次或多次 |
| X+? | X,一次或多次 |
| X{n}? | X,恰好 n 次 |
| X{n,}? | X,至少 n 次 |
| X{n,m}? | X,至少 n 次,但是不超过 m 次 |
例子
package com.xiaostudy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyPattern {
public static void main(String[] args) {
}
private static void demo_Reluctant() {
// 检验规则,单个字母,“+”表示:0次或多次,后面多加一个“?”与不加的区别是:不加的话表示只匹配一次,加的话表示匹配多次
String regex = ".+?222";
// 要检验的对象
String str = "xx222xx222xx222xx222";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
while (matcher.find())
System.out.println(matcher.start() + "=====" + matcher.end());
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_aBAb() {
// 检验规则,字母集,“+”表示:0个或多个
String regex = "[abcd]+";
// 要检验的对象
String str = "adbcdbaDACDBDAC";
// 编译正则表达式,不区分大小写
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_abcd() {
// 检验规则,字母集,“+”表示:0个或多个
String regex = "[abcd]+";
// 要检验的对象
String str = "adbcdabdcddbadbc";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_123no() {
// 检验规则,非数字集,“+”表示:0个或多个
String regex = "[^1-9]+";// 等价于\\D+
// 要检验的对象
String str = "+sdoi#$@%@#";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_123() {
// 检验规则,数字集,“+”表示:0个或多个
String regex = "[1-9]+";// 等价于\\d+
// 要检验的对象
String str = "123";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_2() {
// 检验规则,单个数字
String regex = "[1-9]";
// 要检验的对象
String str = "2";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_nm() {
// 检验规则,单个字母,“{n,m}”表示:出现n次到m次之间,包括他们本身
String regex = "x{3,5}";
// 要检验的对象
String str = "xxxxx";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_n0() {
// 检验规则,单个字母,“{n,}”表示:出现n次或以上
String regex = "x{3,}";
// 要检验的对象
String str = "xxxx";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_n() {
// 检验规则,单个字母,“{n}”表示:就出现n次
String regex = "x{3}";
// 要检验的对象
String str = "xxx";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_xxx0() {
// 检验规则,单个字母,“+”表示:0次或多次
String regex = "x+";
// 要检验的对象
String str = "xxx";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_xxx() {
// 检验规则,单个字母,“*”表示:一次或多次
String regex = "x*";
// 要检验的对象
String str = "xxx";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_x_01() {
// 检验规则,单个字母,“?”表示:一次或一次都没有
String regex = "x?";
// 要检验的对象
String str = "x";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_00() {
// 检验规则,单个字母,“.”表示:任何字符
String regex = ".";
// 要检验的对象
String str = "x";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
private static void demo_x() {
// 检验规则,单个字母
String regex = "x";// 等价于\\w、[a-z]
// 要检验的对象
String str = "x";
// 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 创建匹配器,给定输入与此模式的匹配
Matcher matcher = pattern.matcher(str);
// 匹配,返回结果
boolean b = matcher.matches();
if (b)
System.out.println(true);
else
System.out.println(false);
}
}
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!