Java正则表达式
什么是正则表达式
Regular Expression(正则表达式)是用于匹配指定Pattern(模式、规则)的语句。常用于检索、替换那些符合某个模式(规则)的文本。
java正则语法
正则表达式的规则(pattern)在java官方API的
Pattern
中有详细的讲解。
Character(字符)
Construct | Match | description |
---|---|---|
x | The character x | 除了特殊字符外,普通字符就是表示匹配该字符本身。 如x 表示匹配字符x |
\t | The tab character ('\u0009') | 匹配制表符 |
\r | The character x | 匹配回车符号 |
\n | The newline (line feed) character ('\u000A') | 匹配换行符 |
Character Class (字符类)
Construct | Match | description |
---|---|---|
[abc] | a, b, or c (simple class) | 匹配a或b或c字符 |
[^abc] | Any character except a, b, or c (negation) | 匹配非a,b,c的任意一个字符 |
[a-zA-Z] | a through z or A through Z, inclusive (range) | 匹配a-z(26字母)或A-Z(26个字母)的任意一个字符 |
[a-d[m-p]] | a through d, or m through p: [a-dm-p] (union) | 并集(同上),匹配a-z(26字母)或A-Z(26个字母)的任意一个字符 |
[a-z&&[def]] | d, e, or f (intersection) | 交集,匹配a-z和def的交集一个字符,即def |
[a-z&&[^bc]] | a through z, except for b and c: [ad-z] (subtraction) | 交集+非,匹配a-z且不是bc的一个字符 |
[a-z&&[^m-p]] | a through z, and not m through p: a-lq-z | 交集,匹配a-z且不是m-p的一个字符,即[a-lq-z] |
Predefined character classes(预定义字符)
预定义字符就是元字符,具有特殊含义的字符。
Construct | Match | description |
---|---|---|
. | Any character (may or may not match line terminators) | 匹配任意字符 |
\d | A digit: [0-9] | digit, 匹配任意数字字符0-9 |
\D | A non-digit: [^0-9] | non-digit, 匹配任意非数字字符,[^0-9] |
\w | A word character: [a-zA-Z_0-9] | 匹配单词字符,a-z,A-Z,0-9,_ 相当于[a-zA-Z_0-9] |
\W | A non-word character: [^\w] | 匹配非单词字符 |
\s | A whitespace character: [ \t\n\x0B\f\r] | 匹配空白字符,回车、换行、制表符 |
\S | A non-whitespace character: [^\s] | 匹配非空白字符 |
Boundary matcher(边界匹配器)
Construct | Match | description |
---|---|---|
^ | The beginning of a line | 行的开始 |
& | The end of a line | 行的结束 |
\b | A word boundary | 单词边界符号 |
\B | A non-word boundary | 非单词边界符号 |
Greedy quantifiers(贪婪匹配量词)
Construct | Match | description |
---|---|---|
X? | X, once or not at all | 匹配X0次或者1次 |
X* | X, zero or more times | 匹配X任意多次 |
X+ | X, one or more times | 匹配X至少一次 |
X{n} | X, exactly n times | 匹配X恰好n次 |
X{n,} | X, at least n times | 匹配X大于等于n次 |
X{n,m} | X,匹配X大于等于n次,少于等于m次 |
TIP
贪婪匹配量词的意思是:。与之相对的还有:A,B。
Logical operators(逻辑运算符)
Construct | Match | description | |
---|---|---|---|
XY | X followed by Y | 逻辑与,X后面接着Y | |
X | Y | Either X or Y | 逻辑或,X或者Y |
(X) | X, as a capturing group | 捕获组 |
String类相关的正则表达式
几乎所有的编程语言都支持正则表达式,且被归类为字符串处理。在java也不例外,java的String类中,提供丰富的正则相关的方法。
String类中正则相关方法:
- matches
- split
- replaceAll
matches匹配方法
方法签名为,public boolean matches(String regex)
,用于判断当前字符串是否和正则匹配。
public class RE01StringMatchesTest {
/**
* String.matches()
*/
@Test
public void testStringMatches() {
// 输入手机号码
String phoneInput = "139500828900";
// 判断手机号码是否正确
// 假设手机号码正确的定义是: 11位数, 1开头
System.out.println(phoneInput.matches("1\\d{10}"));
}
}
split,分割
方法签名为:
public String[] split(String regex)
作用是,对正则匹配到的字符作为分隔符,对字符串进行分隔。
/**
* 字符串分割功能
*/
@Test
public void testREStringSplit() {
String dateRange = "20200101,20200103-20200125";
String[] dateArr = dateRange.split("[,\\-]");
System.out.println(Arrays.asList(dateArr));
}
replaceAll,替换
方法签名为:
public String replaceAll(String regex,String replacement)
作用是,对匹配到的字符串进行替换。
/**
* 字符串正则的替换功能
*/
@Test
public void testREStringReplaceAll() {
// 把字符串中的数字替换为*
String str = "hel222lo123wor33ld";
String replacedStr = str.replaceAll("\\d", "*");
System.out.println(replacedStr);
}
使用Pattern,Matcher执行正则
Java中除了可以使用String的实例方法(matches, split, replaceAll)中使用正则,还专门为正则提供了Pattern, Matcher更为专业的来进行
字符串的正则操作
其中,Pattern表示的是正则表达式的规则。而Matcher是Pattern(规则)
对字符串作用后的具体的操作。
Pattern
使用正则的第一步,编译出Pattern(规则)
// 编译出正则表达式
private static final Pattern pattern = Pattern.compile("\\d+");
Matcher
使用java 正则第二步,把编译好的Pattern(规则)应用到字符串中。
private final Pattern findThreeWorldPattern = Pattern.compile("\\b\\w{4}\\b");
@Test
public void testPattern() {
// 搜索字符串中,长度为4的单词,遍历输出
Matcher matcher = findThreeWorldPattern.matcher("Today is Sunday, And I study java regular expression");
while (matcher.find()) {
String threeAlphabet = matcher.group();
System.out.println(threeAlphabet);
}
}