java代码运用正则表达式

符号的解释

在这里插入图片描述
在这里插入图片描述

简单案例

public static void main(String[] args) {

		String password = "jkllk111111111111";
		boolean matches = password.matches("[a-zA-Z0-9]{8,16}");// [8,16]
		System.out.println(matches);

		// 电话号码
		String phone = "12000000000";
		boolean mat = phone.matches("1[3-9]\\d{9}");
		System.out.println(mat);

		// 2.要求去掉字母
		String str = "121dgsdg3dgsdg122dgsdg14354sdgsdgasdghfj";
		String replaceAll = str.replaceAll("[a-zA-Z]", "");
		System.out.println(replaceAll);

		// 3.拆分字符
		String str1 = "fsgsdgdjlgoeto323fds\\dg4345ghfg43dsjfsdgs2314hfgh9306";
		String[] split = str1.split("\\d+");
		System.out.println(Arrays.toString(split));
		
		//4.判断字符串是否是小数,如果是则转换成double类型
		String str2 ="051.3";
		String reg = "\\d+.\\d+";
		
		if(str2.matches(reg)) {
			System.out.println(Double.parseDouble(str2));
		}
		


	}

在开发中运用

方法一

Pattern pattern = Pattern.compile(“正则表达式”);
pattern.matcher(“校验的str”).matches();//matches()返回boolean值
案例:个人建议优先选方法一

if(packageInfo.getThirdPartyNetworkCode() != null){
            String pmsCodeRegular = "[a-zA-Z]{3}";
            Pattern pattern = Pattern.compile(pmsCodeRegular);
            if(pattern.matcher(packageInfo.getThirdPartyNetworkCode()).matches()){
                zhuoZhiPackageQO.setPoa(packageInfo.getThirdPartyNetworkCode());
            }else {
                throw new ApplicationRuntimeException("xxxxxxxxxxx");
            }
        }

方法二

Pattern pattern = Pattern.compile(“正则表达式”);
Matcher matcher = pattern.matcher(“校验的str”);
if(matcher.find()){} //matcher.find()返回boolean值
案例:

 String str = "sdf";
        if(str.length()==3) {
            Pattern pattern = Pattern.compile("[a-zA-Z]{3}");
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                System.out.println("yes");
            } else {
                System.out.println("no");
            }
        }else {
            System.out.println("bug");
        }

没有给校验的字段设置长度可能在开发中可能有bug,比如正则表达式为:[a-zA-Z]{3},那么abc符合正常,但是abcdef也符合,这是bug
解决方案:给校验的字段增加长度限制

matches 和 lookingAt 方法比较

matches() 和 lookingAt() 方法都用来尝试匹配一个输入序列模式。不同的是 matches() 要求整个序列都匹配,而 lookingAt() 不要求;
lookingAt() 方法虽然不需要整句都匹配,但是需要从第一个字符开始匹配;
这两个方法经常在输入字符串的开始使用。

public void matchAndLookingExample() {
	String REGEX = "foo";
	String INPUT = "foooooooooo";
	String INPUT2 = "ooooofoooooo";
	
	Pattern pattern = Pattern.compile(REGEX);
	Matcher matcher = pattern.matcher(INPUT);
	Matcher matcher2 = pattern.matcher(INPUT2);
	
	System.out.println("lookingAt(): "+matcher.lookingAt());
	System.out.println("matches(): "+matcher.matches());
	System.out.println("lookingAt(): "+matcher2.lookingAt());
}
输出结果:
	lookingAt(): true	// 开头匹配
	matches(): false	// 不是整个序列都匹配
	lookingAt(): false	// 开头不匹配

常用的正则表达式

说明正则表达式
校验数字的表达式 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
数字^ [0-9]*$
n位的数字^\d{n}$
至少n位的数字^\d{n,}$
m-n位的数字^\d{m,n}$
非负整数^\d+$
非正整数^((-\d+)
英文和数字^ [A-Za-z0-9]+$
长度为3-20的所有字符^.{3,20}$
由26个英文字母组成的字符串^ [A-Za-z]+$
由数字、26个英文字母或者下划线组成的字符串^\w+$ 或 ^\w{3,20}$
中文、英文、数字包括下划线^ [\u4E00-\u9FA5A-Za-z0-9_]+$
中文、英文、数字但不包括下划线等符号^ [\u4E00-\u9FA5A-Za-z0-9]+$
可以输入含有^%&’,;=?$"等字符[^%&’,;=?$\x22]+
禁止输入含有~的字符[^~\x22]+
特殊需求表达式 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Email地址^\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*$
域名[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
InternetURL[a-zA-z]+://[^\s]* 或 ^http://([\w-]+.)+[\w-]+(/[\w-./?%&=]*)?$
手机号码^(13[0-9]
电话号码^((\d{3,4}-)
身份证号(15位、18位数字)^\d{15}
短身份证号码(数字、字母x结尾)^([0-9]){7,18}(x
帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)^ [a-zA-Z][a-zA-Z0-9_]{4,15}$
密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线)^ [a-zA-Z]\w{5,17}$
日期格式^\d{4}-\d{1,2}-\d{1,2}
中国邮政编码[1-9]\d{5}(?!\d) (中国邮政编码为6位数字)
腾讯QQ号[1-9][0-9]{4,} (腾讯QQ号从10000开始)
IP地址\d+.\d+.\d+.\d+ (提取IP地址时有用)
中文字符的正则表达式[\u4e00-\u9fa5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pedro的程序时刻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值