Java基础-正则表达式的基本使用

正则表达式

正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。

下面介绍正则表达式的基本应用:
集合类字符匹配

ReGex.java文件

package Object;

import java.util.Scanner;

public class ReGex {
	public static void main(String[] args) {
		// 正则表达式的几大类

		Scanner sc = new Scanner(System.in);

		// 简单类[abc] 只能出现一个字符 该字符必须是[]里面的字符其中之一
		// String Regex = "[abd]";
		
		// 负向类[^abc] 只能出现一个字符 该字符必须是[]里面的字符其中之一
		// String Regex = "[^abd]";

		// 范围类[a-z] 只能出现一个字符该字符是[]里面的字母范围内的一个
		// String Regex = "[a-k]";

		// 范围类[a-zA-Z]或者[a-z[A-Z] 只能出现一个字符该字符是[]里面的字母范围内的一个或者[]里面后面范围内的其中一个
		// 要么是[a-z]要么是[A-Z]
		// String Regex = "[a-z[A-Z]";

		// 范围类[a-z&&[abc]]交集 范围类和简单类交集 只能取abc
		// String Regex = "[a-z&&[abc]]";

		// 范围类[a-z&&[A-Z]]交集 范围类和范围类交集 什么都没有
		// String Regex = "[a-z&&[A-Z]]";

		// 范围类[a-z&&[^A-Z]]交集 范围类和负向类交集 [a-z]
		// String Regex = "[a-z&&[^A-Z]]";

		String Regex = "[a-z&&[^A-Z]]";

		while (true) {
			System.out.println("请输入要比较的字符串:");
			String str = sc.nextLine();
			System.out.println(str.matches(Regex));

		}
	}
}

预定义字符匹配
ReGex1.java文件

package Object;

import java.util.Scanner;

public class ReGex1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		// .匹配任何字符 数量是一个 不匹配\r \n
		// String regex = ".";

		// 正则表达式的转义字符 表示[0-9] 纯数字 只有一位
		// String regex = "\\d"; // \\d转义为\d

		// 非数字 匹配一个
		// String regex = "\\D";

		// 空白字符 匹配一个 \r \n \t \x0B " " 不匹配回车
		//String regex = "\\s";
		
		//非空白字符  匹配一个
		//String regex = "\\S";
		
		//单词字符 数字 字母 下划线
		//String regex = "\\w";
		
		//非单词字符
		String regex = "\\W";	
		while (true) {
			System.out.println("请输入要比较的字符串:");
			String str = sc.nextLine();
			System.out.println(str.matches(regex));

		}
	}

}

数量词匹配
greedy数量词的使用
ReGex2.java文件

package Object;

import java.util.Scanner;

public class ReGex2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		// x? x中的字符出现0次或者1次
		// String regex = "[abc]?";

		// x* x中的字符出现任意次
		// String regex = "[abc]*";

		// x+ x中的字符出现一次或者多次 至少出现一次
		//String regex = "[abc]+";
		
		//x{n} x中的字符恰好出现n次
		//String regex = "[abc]{3}";
		
		//x{n,} x中的字符至少出现n次
		//String regex = "[abc]{3,}";
		
		//x{n,m} x中的字符至少出现n次最多出现m次
		String regex = "[abc]{3,4}";
		while (true) {
			System.out.println("请输入要匹配的字符:");
			String str = sc.nextLine();
			System.out.println(str.matches(regex));
		}
	}

}

正则表达式 小练习
手机号码的一个简单验证:
规则:
 第一位只能是1 第二位不能是1和2 后面9位只要是数字就符合

package Object;

import java.util.Scanner;

public class ReGex {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String Regex = "[1][3-9][\\d]{9}";
		// 第一位只能是1 第二位不能是1和2 后面9位只要是数字就符合

		while (true) {
			System.out.println("请输入要要检查的手机号:");
			String str = sc.nextLine();
			if (str.matches(Regex)) {
				System.err.println("手机号格式输入正确!!!");
				break;
			} else {
				System.out.println("手机号格式输入错误!请重新输入!");
			}

		}
	}
}

字符串的模糊匹配
 对于一些字符串实现通过正则表达式来实现对字符串中的一些字符的提取和转换。

package Object;

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

public class ReGex {
	public static void main(String[] args) {
		String str = "56xxh-85kkE-kk84-$%^pp-87aa";

		// 模糊匹配 模板分组 取出一部分
		Pattern pattern = Pattern.compile("\\d+[a-zA-Z]+");
		
		//模板分组 取出一部分
		//Pattern pattern = Pattern.compile("(\\d+)([a-zA-Z]+)");
		
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			//查找到的位置
			// System.out.println("头位置:"+matcher.start()+";尾位置:"+matcher.end());
			
			//查找到的字符串
			System.out.println("找到的字符串为:" + str.substring(matcher.start(), matcher.end()));
			
			//取出指定组的数据
			System.out.println("组:"+matcher.group());//匹配到的当前的字符串
			System.out.println("第一组(数字组):"+matcher.group(1));//匹配到的当前的字符串的字母组
			System.out.println("第二组(数字组):"+matcher.group(2));//匹配到的当前的字符串的字符组
		}
		System.out.println("找完了!");

		// 替换
		// System.out.println(matcher.replaceAll("hehe"));
	}

	public static void main1(String[] args) {
		// 根据固定模板去查找
		String str = "hehe-Hehe-qksmhehshehe-HEHE";
		// 编译模板
		// Pattern compile = Pattern.compile("hehe");
		Pattern compile = Pattern.compile("hehe", Pattern.CASE_INSENSITIVE);// 忽略大小写查找
		// 将模板对象和字符串进行匹配 返回matcher对象
		Matcher matcher = compile.matcher(str);

		// 查找
		while (matcher.find()) {// 只有find方法返回true才能继续打印 位置
			System.out.println("头位置:" + matcher.start() + ";尾位置:" + matcher.end());
		}
		System.out.println("匹配完成!!");

		// 全局固定替换
		// System.out.println(matcher.replaceAll("1"));

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值