package com.atguigu.springcloud.utils;
import jdk.nashorn.internal.objects.annotations.Where;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 42243
* @create 2022/5/1 13:29
*/
public class RegularExpression {
public static void main(String[] args) {
regular();
}
/**
* 1.限定符:出现次数
* a{3,4} 1+ 贪婪匹配,尽可能匹配多的。 a? 匹配0次到1次。 * 重复0次到n次。 {n,} 大于 n
* 2.选择匹配符:
* "han|韩|寒" 满足其中一个就行
* 3.分组组合和反向引用符 :
* 3.1非命名分组:(\\d\d)(\\d\d)
* matcher.group(0) 得到匹配到的字符串
* matcher.group(1) 得到匹配到的第一个分组内容
* matcher.group(2) 得到匹配到的第二个分组内容
* 3.2 命名分组:(?<g1>\\d\\d)(?<g2>\\d\\d)
* matcher.group(g1) 得到匹配到的第一个分组内容
* matcher.group(g2) 得到匹配到的第二个分组内容
* 3.3 非捕获组 李(?:军|剑|帅) = 李军|李剑|李帅
* 4.特殊字符
* 5.字符匹配符:
* [fegh]其中任意一个字符 , [^fegh] 非 , - 连接符 [A-Z]
* \\. 除\n以外的任意字符 \\d单个数据相当于[0-9] \\D 相当于[^0-9]
* \\w [0-9a-zA-Z] \\W [^0-9a-zA-Z] (?i)abc 不区分大小写 \\s 匹配任意空格
* \\S 非空格
*
* 6. 定位符 :规定字符出现的位置
* ^ 起始字符 。 $ 指定结束字符 \\b 以什么字符串结尾 han\\b \\B 不以什么字符结尾
*
* 7. 静态方法:
* matcher.start matcher.end matcher.subing 展示区间的字符串 matcher.matcher 整体匹配
* 8. 反向引用
* (\\d)(\\d)\\2\\1 = 5225
*
* 9. 结巴程序
*
* */
public static void regular(){
// String str = "零点十分发达看" ;
// String str = "122456955" ;
// String str = "https://search.bilibili.com/all?vt=91287186&keyword=java%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F&from_source=webtop_search&spm_id_from=333.1007" ;
String str = "我......要要要...你你在我身旁.";
//汉字
// String pattern = "^[\u0391-\uffe5]+$"; // +一到多
//邮政编码
// String pattern = "^[1-9]{5}$";
//qq号码
// String pattern = "^[1-9]\\d{4,9}$";
//手机号码
// String pattern = "^1[3|4|5|8]\\d{9}$";
//URL
// String pattern = "^((http|https)://)([\\w-]+\\.)+[\\w-]+(\\/[\\w-?=&/%.#]*)?$";
//去重 --=====================================
// String pattern = "\\.";
// Pattern p = Pattern.compile(pattern);
// Matcher matcher = p.matcher(str);
// String content = matcher.replaceAll("");
// System.out.println("content="+content);
//
// String pattern2 = "(.)\\1+";
// Pattern compile2 = Pattern.compile(pattern2);
// Matcher matcher2 = compile2.matcher(content);
//
// //使用反向引用 $1 来替换匹配的内容
// String contentReplace = matcher2.replaceAll("$1");
// System.out.println("contentReplace=" + contentReplace);
//去重 --=====================================
//邮箱验证
// String str3="jliwe@qq.com.cn";
// String pattern="^[\\w-]+@([a-zA-Z]+\\.)+[a-zA-Z]+$";
// Pattern compile = Pattern.compile(pattern);
// Matcher matcher = compile.matcher(str3);
// System.out.println(matcher.find()+"==============");
//判断是不是整数或者小数 ,考虑整数负数
// String str4="1";
// String pattern = "^[-+]?([1-9]\\d*|0)(\\.\\d+)?$";
// Pattern compile = Pattern.compile(pattern);
// Matcher matcher = compile.matcher(str4);
//解析url
String str4="https://www.bilibili.com:8080/sfs.htm";
String pattern = "^([a-zA-Z]+)://([a-zA-Z.]+):(\\d+)[\\w-/]*/([\\w.]+)$";
Pattern compile = Pattern.compile(pattern);
Matcher matcher = compile.matcher(str4);
while (matcher.find()) {
System.out.println("整体匹配:" + matcher.group(0) + "=================");
System.out.println("协议:" + matcher.group(1) + "=================");
System.out.println("域名:" + matcher.group(2) + "=================");
System.out.println("端口:" + matcher.group(3) + "=================");
System.out.println("文件:" + matcher.group(4) + "=================");
}
// System.out.println(matcher.find());
// while (matcher.find()){
// System.out.println(matcher.group(0)+"-===========");
// }
}
}
浅学正则表达式
最新推荐文章于 2024-07-10 15:25:29 发布