一、简介
Java
正则表达式是一种强大的工具,用于在字符串中匹配和操作文本模式。它可以用于验证输入的有效性、搜索和替换字符串中的特定模式,以及提取所需的信息。在Java
中,使用java.util.regex
包来处理正则表达式。
二、元字符
Java正则表达式中的元字符是一些特殊字符,具有特殊的含义和功能。
.
匹配任意单个字符,除了换行符。
eg:正则表达式a.b
可以匹配字符串中的aab
、acb
、axb
等^
匹配输入字符的开始位置
eg:正则表达式^abc
可以匹配以abc
开头的字符串$
匹配输入字符的结束位置
eg:正则表达式abc$
可以匹配以abc
结尾的字符串*
匹配前面的元素零次或多次
eg:正则表达式A*
可以匹配字符串中的AA
、AAA
等+
匹配前面的元素一次或多次
eg:正则表达式A+
可以匹配字符串中的A
、AA
、AAA
等
6.?
匹配前面的元素零次或一次
eg:正则表达式A?
可以匹配字符串中的A
[]
定义一个字符集,匹配其中的任意一个字符
eg:正则表达式[a-r]
代表a~r
中的任意一个字符[^]
定义一个否定字符集,匹配除了其中的字符之外的任意字符
eg:正则表达式[^456]
代表除了4
、5
、6
之外的任意一个字符()
标记一个子表达式的开始和结束位置
eg:正则表达式(ab)+
可以匹配ab
、abab
、ababab
等|
只是两个或多个选择之间的一个
eg:正则表达式apple|orange
可以匹配apple
或orange
\
转义字符,用于匹配特殊字符本身
eg:正则表达式\.
可以匹配句号.
三、字符类
在Java
正则表达式中,字符类可以匹配指定的一组字符。
[abc]
匹配任何一个a
、b
或c
字符[^abc]
匹配任何非a
、b
或c
字符[a-z]
匹配任何小写字母[A-Z]
匹配任何大写字母[0-9]
或\d
匹配任何数字\D
匹配任何非数字字符\s
匹配任何空白字符,包括空格、制表符、换页符等\S
匹配任何非空白字符\w
匹配任何字母、数字或下划线字符\W
匹配任何非字母、数字或下划线字符
四、限定修饰符
在Java正则表达式中使用限定修饰符来限定前面的元素必须出现的次数。
1.?
0次或1次
2. *
0次或多次
3. +
一次或多次
4. {n}
正好出现n次
5. {n,}
至少出现n次
6. {n,m}
至少出现n~m次
五、正则表达式
Java
中通过java.util.regex
包来进行正则表达式操作,这个包中的主要类是Pattern
和Matcher
。
Pattern
用于定义要在文本中搜索的模式,Matcher
用于通过解释Pattern对象进行模式匹配和搜索,
1 使用方法
- 定义正则表达式,首先定义一个字符串,该字符串包含想要在文本中的搜索的模式。这个模式可以包含各种元字符、字符类和限定符。
- 编译正则表达式:使用
Pattern.compile()
方法将字符串形式的正则表达式编译成一个Pattern
对象
String regex="abc";
Pattern pattern = Pattern.compile(regex);
- 创建
Matcher
对象:使用Pattern
对象的matcher()
方法创建一个Matecher
对象,该对象可以对一个输入字符串进行模式匹配
String input = "abcdef";
Matcher matcher = pattern.matcher(input);
- 执行匹配操作:使用Matcher对象的各种方法(如
matches()
、find()
、replaceAll()
等)执行匹配
boolean isMatch = matcher.matches();//检查整个输入字符串是否匹配正则表达式
boolean isFound = matcher.find();//查找输入字符串的下一个匹配项
- 处理匹配结果:根据匹配结果执行相应操作。(打印匹配项的位置、替换所有匹配项等)
import java.util.regex.*
public class Main{
public static void main(String[] args){
//定义正则表达式
String regex="abc";
//定义输入字符串
String input="abcdef";
//编译正则表达式
Pattern pattern = Pattern.compile(regex);
//创建Matcher对象
Matcher matcher = pattern.matcher(input);
//执行匹配操作并处理匹配结果
while(matcher.find()){
System.out.println("Found match at:"+matcher.start());
}
}
}
2.Pattern类
Java
中Pattern
类是正则表达式的编译表示形式,提供一系列的方法来处理正则表达式
compile(String regex)
编译给定的正则表达式并返回一个Pattern对象
Pattern pattern = Pattern.compile("a*b");
matcher(CharSequence input)
创建一个匹配器,匹配给定的输入与给定模式
Matcher matcher=pattern.matcher("aaaab");
boolean matches=matcher.matches();
System.out.println("Matches"+matches);//输出:Matches:true
3. split(CharSequence input)根据此模式的匹配项来拆分给定的输入序列
Pattern splitPattern = Pattern.compile("\\s");
String words= splitPattern.splt("Hello World");
for(String word : words){
System.out.println(word);
}
pattern()
返回正则表达式模式
System.out.println("Pattern:"+pattern.pattern());//输出:Pattern:a*b
matches(String regex,CharSequence input)
编译给定的正则表达式并尝试将给定的输入与其匹配
boolean isMatch = Pattern.matches("\\d","12345);
System.out.println("Is Match:"+isMatch);//输出:Is Match:true
3.Matcher类
Java
中Matcher
类是Java
正则表达式的核心类,提供了一系列的方法来操作和处理正则表达式。
boolean matches()
尝试将整个区域与模式进行匹配
Pattern pattern = Pattern.compile("test");
Matcher matcher = pattern.matcher("test")
boolean matches = matcher.matches();//返回true
boolean find()
尝试查找与模式匹配的输入序列的下一个子序列
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("hello123world");
while(matcher.find()){
System.out.println(matcher.group());//输出123
}
boolean find(int start)
从指定的索引开始,尝试查找与模式匹配的输入序列的写一个子序列
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("hello123world456");
matcher.find(3);
System.out.println(matcher.group());//输出123
String group()
返回由以前匹配操作所匹配的输入子序列
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("hello123world");
if (matcher.find()) {
System.out.println(matcher.group()); // 输出 123
}
int start()
返回以前匹配的初始索引
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("hello123world");
if (matcher.find()) {
System.out.println(matcher.start()); // 输出 5
}
6. int end()
返回最后匹配字符之后的偏移量
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("hello123world");
if (matcher.find()) {
System.out.println(matcher.end()); // 输出 8
}
- 正则表达式常用操作
● 匹配:检查一个字符串是否匹配一个特定的模式
String pattern="abc";
String text="abcdef";
boolean matches=Pattern.matches(pattern,text);//false
● 查找:在一个字符串中查找与一个特定模式匹配的子字符串。
String pattern = "\\d+";
String text = "abc123def456";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println(m.group());
}
● 替换:在一个字符串中替换与一个特定模式匹配的子字符串
String pattern = "\\d+";
String text = "abc123def456";
String replacement = "XYZ";
String result = text.replaceAll(pattern, replacement);
● 分割:使用一个特定的模式来分割一个字符串
String pattern = ",";
String text = "abc,def,ghi";
String[] parts = text.split(pattern);