目录
一、分组、捕获、反向引用
1.捕获分组
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class RegExp07 {
public static void main(String[] args) {
String content = "1998dvnkidn2005";
//匹配非边界的zhang
//String regStr = "\\d\\d\\d\\d";
//String regStr = "(\\d\\d)(\\d\\d)";
String regStr = "(?<group1>\\d\\d)(?<group2>\\d\\d)";//命名分组
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
//System.out.println("找到" + matcher.group(0));
System.out.println("第一个分组内容" + matcher.group(1));
System.out.println("第一个分组内容[通过组名]" + matcher.group("group1"));
System.out.println("第二个分组内容" + matcher.group(2));
System.out.println("第二个分组内容[通过组名]" + matcher.group("group2"));
}
}
}
2、非捕获分组
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class RegExp08 {
public static void main(String[] args) {
//String content = "你好jack你好rose你好bob";
String content = "windows2000windows3.1windows95";
//String regStr = "你好(?:jack|rose|bob)";//等价于非捕获分组
//String regStr = "windows(?=95|98|2000)";//非捕获匹配,匹配windows2000中的windows,但不匹配windows3.1中的windows
String regStr = "windows(?!95|98|2000)";//非捕获匹配,匹配windows3.1中的windows,但不匹配windows2000中的windows
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("找到:" + matcher.group(0));
}
}
}
3、非贪婪匹配
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class RegExp09 {
public static void main(String[] args) {
String content = "hello111111abc";
//String regStr = "\\d+";//默认贪婪匹配。尽可能匹配多的
String regStr = "\\d+?";//非贪婪匹配
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("找到:" + matcher.group(0));
}
}
}
4、反向引用
1)需求引出
2)反向引用案例
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class RegExp12 {
public static void main(String[] args) {
String content = "hello33333 jack5555 jack11 jack22 tom11 xxx yyy AAA 1661 12321-222333444";
//String regStr = "(\\d)\\1";//匹配连续两个相同数字
//String regStr = "([a-z])\\1";//匹配两个连续相同小写字母
//String regStr = "(\\d)\\1{4}";//匹配五个连续相同数字
//String regStr = "(\\d)(\\d)\\2\\1";//匹配个位与千位相同,十位与百位相同的数
String regStr = "\\d{5}-(\\d)\\1{2}(\\d)\\2{2}(\\d)\\3{2}";//匹配形如"12321-222333444"的数
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println(matcher.group(0));
}
}
}
二、正则应用实例
1.验证汉字、邮编、手机号等
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class RegExp10 {
public static void main(String[] args) {
//String content = "程序员飞扬";//汉字
//String content = "100199";//邮编
//String content = "826350203";//qq号
String content = "13845436666";//手机号
//验证是否是汉字
//String regStr = "^[\u0391-\uffe5]+$";
//匹配邮编,要求非零数字开头的六位数
//String regStr = "^[1-9]\\d{5}$";
//匹配邮编,要求非零数字开头的六位数
//String regStr = "^[1-9]\\d{5}$";
//匹配QQ号码,要求非零数字开头的5-10位数字
//String regStr = "^[1-9]\\d{4,9}$";
//手机号码,要求13,14,15,18开头的11位数
String regStr = "^1[3|4|5|8]\\d{9}$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("验证通过");
}
}
}
2.验证复杂url
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class RegExp12 {
public static void main(String[] args) {
//String content = "https://blog.csdn.net/lu_xin5056?type=blog";//url
String content = "https://www.infoq.cn/article/apache-shiro/";//url
String regStr = "^((https|http)://)?([\\w-]+\\.)+[\\w-]+(\\/[\\w-?=&/%.]*)?$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("验证通过");
}
}
}
三、正则表达式三个常用类
1.Pattern类
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class PatternMethod {
public static void main(String[] args) {
//String content1 = "hello123helloabc89778";
String regStr1 = "\\w*";
String content = "信用卡";
String regStr = "信用卡|产线|VIP";
boolean matches = Pattern.matches(regStr, content);
System.out.println("整体匹配:" + matches);//true
}
}
2.Matcher类
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class MatcherMethod {
public static void main(String[] args) {
String content = "hello edu jack tom hello smith hello";
//String regStr = "hello";
String regStr = "\\w.*";
Pattern compile = Pattern.compile(regStr);
Matcher matcher = compile.matcher(content);
while(matcher.find()){
System.out.println(matcher.start());
System.out.println(matcher.end());
System.out.println("找到" + content.substring(matcher.start(),matcher.end()));
}
//整体匹配方法,常用于校验某个字符串是否满足某个规则
boolean matches = matcher.matches();
System.out.println(matches);
System.out.println("===============================");
String regStr1 = "hello";
Pattern compile2 = Pattern.compile(regStr1);
Matcher matcher2 = compile2.matcher(content);
String contentNew = matcher2.replaceAll("你好");
System.out.println(contentNew);
}
}
四、案例
1、结巴去重
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class RegExp13 {
public static void main(String[] args) {
String content = "我我..要要要...学学学学....编程java";
//1. 将所有"."替换为空字符串
String s = Pattern.compile("\\.").matcher(content).replaceAll("");
//2. "(.)\\1+",分组捕获重复字符串
// "$1",反向引用,替换所有的捕获的字符串
//String s1 = Pattern.compile("(.)\\1+").matcher(s).replaceAll("$1");//方法1
String s1 = s.replaceAll("(.)\\1+","$1");//方法2
System.out.println(s1);//输出:我要学编程java
}
}
2.String类使用正则(非常实用!!!)
①正则替换
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class StringReg {
public static void main(String[] args) {
//将所有JDK1.3或JDK1.4替换为JDK
String content = "2000年5月,JDK1.3、JDK1.4和J2SE1.3相继发布,几周后其获得了Apple公司Mac OS X的工业标准的支持。" +
"2001年9月24日,J2EE1.3发布。2002年2月26日,J2SE1.4发布。自此Java的计算能力有了大幅提升," +
"与J2SE1.3相比,其多了近62%的类和接口。在这些新特性当中,还提供了广泛的XML支持、安全套接字(Socket)" +
"支持(通过SSL与TLS协议)、全新的I/OAPI、正则表达式、日志与断言。2004年9月30日,J2SE1.5发布,成为Java语言发展史上的又一里程碑。" +
"为了表示该版本的重要性,J2SE 1.5更名为Java SE 5.0(内部版本号1.5.0)," +
"代号为“Tiger”,Tiger包含了从1996年发布1.0版本以来的最重大的更新,其中包括泛型支持、基本类型的自动装箱、" +
"改进的循环、枚举类型、格式化I/O及可变参数。";
String newContent = content.replaceAll("JDK1\\.3|JDK1\\.4", "JDK");
System.out.println(newContent);
}
}
②正则验证
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class StringReg {
public static void main(String[] args) {
String content2 = "13888889999";
boolean matches = content2.matches("(138|139)\\d{8}");//验证手机号开头
if(matches){
System.out.println("验证成功");
}else{
System.out.println("验证失败");
}
}
}
输出:成功
③正则分割
/**
* @author:程序员飞扬
* @公众hao:程序员飞扬
* @description:
*/
public class StringReg {
public static void main(String[] args) {
String content3 = "hello#程序员-飞扬18smith~上海";
String[] split = content3.split("#|-|\\d+|~");
for (String s : split) {
System.out.println(s);
}
}
}
输出:
hello
程序员
飞扬
smith
上海