2021-02-01

day_24_正则表达式

2.1 概述

  • 正则表达式,定义了字符串的模式,可以用来搜索,编辑或处理文本,并不仅限于某一种语言 在任何语言中都有,但是有细微的差别
  • java中在1.4推出java.util.regex包,为我们提供了java使用正则表达式的应用平台
  • java中 \ 为转移符 把有意义字符转换为无意义字符
  • 但是在正则表达式中 \ 也是转移符,把有意义字符转换为无意义字符
  • 所以 在java中使用正则表达式中的 \ 的时候 需要使用 两个 \

练习
1 匹配整数和小数

^\d 以数字打头
.匹配任意字符,需要转义 .
? 出现0次或1次

  • 出现1到n次
    () 把.和\d+ 看做一个整体

^\d+ (.\d+)?

2 匹配电话

^\d{11}$

^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$

2.4 Java中支持正则表达式的类

  • 在java.util.regex下,有三个正则表达式相关的类
  •  PatternSyntaxException : 正则表达式异常类
    
  •  Pattern : 正则表达式类,只能做简单操作
    
  •  Matcher : 支持强大的正则表达式匹配操作
    

Pattern
正则表达式,定义了字符串的模式,可以用来搜索,编辑或处理文本

  • java中 \ 为转移符 把有意义字符转换为无意义字符
  • 但是在正则表达式中 \ 也是转移符,把有意义字符转换为无意义字符
  • 所以 在java中使用正则表达式中的 \ 的时候 需要使用 两个 \
  • 常用语法 :
  •  \ : 转移符
    
  •  字符取值范围
    
  •  	[abc] : 表示可能是a可能是b也可能是c
    
  •  	[^abc] : 表示不是a,b,c中任意一个
    
  •  	[0-9] : 表示是0到9任意数字
    
  •  	[a-zA-Z] : 表示是大小写字母
    
  •  简洁字符表示
    
  •  	. : 匹配任意字符
    
  •   \d : 表示数字,等于 [0-9]
    
  •   \D : 非数字,等于 [^0-9] 	
    
  •   \s : 表示由空字符组成,等于 [ \t\n\r\x\f]
    
  •  \S : 表示由非空字符组成,等于 [^\s]
    
  •  \w : 表示字母,数字,下划线,等于 [a-zA-Z0-9_]
    
  •  \W : 表示非字母,数字,下划线
    
  •  数量表达式
    
  •  	? : 表示出现0次或1次
    
  •  	+ : 表示出现1次或多次
    
  •  	* : 表示出现任意次
    
  •  	{n} :  表示出现n次
    
  •  	{n,m} : 表示出现n到m次
    
  •  	{n,} : 表示出现n次或n次以上
    
  •  逻辑相关 : 
    
  •  	XY : 表示X后面跟着Y
    
  •  	X|Y : 表示X或Y  food|fa  匹配food或者fa
    
  •  	(food|f)  | a
    
  • ^ : 表示以什么开头
  • $ : 表示以什么结尾
public class PatternTest {
	public static void main(String[]args){
		test1();
		test2();
		
	}
	//String[]split(CharSequence input)Pattern类中的成员方法,用于分隔字符串
	public static void test1() {
		String str = "1.2.3.4.5";
		//创建一个正则表达式对象
		//.匹配任意字符,需要转化为无意义字符
		//\.
		//因为  在Java中 \也是转义字符,所以需要加两个\\
		Pattern pattern =  Pattern.compile("\\.");
		//以 . 分割 返回字符串数组
		String[]strs = pattern.split(str);
		for (String string : strs) {
			System.out.println(string);
		}
		System.out.println("------");
		//String 中的  方法
		String[]strs1= str.split("\\.");
		for (String string : strs1) {
			System.out.println(string);
		}
	}
	//Pattern.matches(String regex,CharSequence input):
	//静态方法   返回值是  布尔型,用于快速匹配字符串(是匹配全部字符串)
	public static void  test2() {
		String str ="12345678910";
		String regex = "\\d{11}";
		//全词匹配,就是整个字符串只有十一个数字,其他没有
		System.out.println(Pattern.matches(regex, str));
		//string中的方法,也是全词匹配
		System.out.println(str.matches(regex));
		
	}

Matcher
Pattern 是Java中正则表达式引擎

  • Matches:匹配器\
  • 三种匹配模式 :
  •  	.匹配任何字符,* 匹配任意次数
    
  •  	matches : 全词匹配
    
  •  	find : 在任意位置均可    .*xxxx .*
    
  •  	lookingAt  : 从前向后匹配   xxxx.*
    
  • 注意 ! 一个matcher对象,和相应的 find/matches/lookingAt 是配对的,
    不要一起使用同一个matcher对象
    如果一定要连用,必须重新打开matcher就可以
    调用相同方法是可以连用的,比如调用多次find方法
public class MatchesTest {
	public static void main(String[]args) {
		test1();
		test2();
		test3();
		
		
	}
	public static void  test1() {
		String regexTel ="\\d{11}";
		String tel = "13113113111a";
		//引擎对象
		Pattern pattern = Pattern.compile(regexTel);
		//匹配器对象
		Matcher matcher = pattern.matcher(tel);
		//全瓷
		System.out.println(matcher.matches());
		//随意
		//重新 打开matches 对象
		matcher = pattern.matcher(tel);
		System.out.println(matcher.find());
		
		//前到后
		matcher = pattern.matcher(tel);
		System.out.println(matcher.lookingAt());
		
	}
	public static void test2() {
		String regexTel = "((.{2,3})的电话是)(\\的{11})";
        String tel = "张三的电话是13113113111李小四的电话是15115115111";
		
		// 引擎对象
		Pattern pattern = Pattern.compile(regexTel);
		// 匹配器对象
		Matcher matcher = pattern.matcher(tel);
		
		// find和group连用可以提取数据的
		// 可以使用() 进行分组,一个() 就是一组
		// 也可以不分组,不分组 就获取匹配到的数据,不能截取数据中的某一部分
		// find匹配,如果字符串中有多个符合条件的数据,匹配到第一个就停止
		// 想要获取下一个匹配的数据,就要再次调用find方法即可
		while (matcher.find()) {
			// 0 和无参 是获取匹配到的数据
			// 1 就是第一组 2 就是第二组
			System.out.println(matcher.group(2) +" : "+matcher.group(3));
			// 匹配元素的起始索引
			// System.out.println(matcher.start());
			// 匹配元素的结束索引
			// System.out.println(matcher.end());
		}
	}
	public static void test3() {
		
	}

利用正则表达式去重

package Regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 叠词匹配   去除重复
 */
public class Test { 
	public static void main(String[]args) {
		String string = "我我我,,,要要要,,要要,,学学学,学,编编编,编编程,程程程,,,程程";	
		//第一步,去掉逗号
		string = string.replaceAll(",", "");
		System.out.println(string);
		//使用find和group 获取数据
		// (.) 任意字符组成, \\1 捕获1次 , 叠词, $1 得到1组,取前面的组1次或多次
		
				// \\1 获取前面组中的数据
				// (\\d)\\1 : 表示连续出现的数字字符 , 比如 11,22,333,44444
				// (\\d)(a)\\1 : 匹配第一个是数字,第二个是a,第三个和第一个是相同的数字 ,比如 1a1 , 2a2, 4a4
				// (\\d)(a)\\2 : 匹配第一个是数字,第二个是a,第三个和第二个是相同的 , 比如 1aa , 2aa , 8aa
		String regex = "(.)(\\1+)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(string);
		// System.out.println(matcher.find());
		while (matcher.find()) {
			// 获取每个字的叠词
			// 而 group (1) 就是只要 .  ,也就是叠词中只要1个
			
			// 所有 
			System.out.println(matcher.group(0));
			// 第一组 我
			System.out.println(matcher.group(1));
			// 第二组   比所有的少一个
			System.out.println(matcher.group(2));
		}
		
		/**
		 * 还原成 我要学编程
		 */
		// $1 就等于 group(1)
		// $1 等于所有去重.因为$1就是group(1) 而我们这个正则表达式中 第一组 就是叠词中的一个
		string = string.replaceAll(regex, "$1");
		
		// $2 就是重复就删一个,因为第二组是叠词中删除一个相同的叠词
		// string = string.replaceAll(regex, "$2");
		System.out.println(string);
	}
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值