正则表达式语法规则-Java

规则:[内容限定]{长度限定}

1.内容限定

在定义限定内容规则时,如果没有指定长度限定,那么默认长度为1。

1.1单个字符限定

[a]:表示当前内容必须是字母a

public class RegexTest {
	public static void main(String[] args) {
		String regex = "[a]";
		//String content = "a";  //返回true
		String content = "b";   //返回false   ,如果是"aa",也是false
		System.out.println(content.matches(regex));
	}

}

1.2范围字符限定

[a-z0-9]:表示内容可以是a-z之间的任意字母或者0-9之间的任意数字,不分先后。

public class RegexTest {
	public static void main(String[] args) {
		String regex = "[a-z]";
		//String regex = "[a-zA-Z0-9]"  //表示a-z大小写和0-9数字
		//String content = "A";  //返回false,如果regex=[a-zA-z]即可为true
		String content = "b";   //返回true   
		System.out.println(content.matches(regex));
	}

}

1.3取反限定

[^abc]:表示内容不能是a或b或c。

public class RegexTest {
	public static void main(String[] args) {
		
		String regex ="[^ab]";
		String content = "b";   //返回false   
		System.out.println(content.matches(regex));
	}

}

2.长度限定

在正则表达式中通过{}限定内容长度。

固定长度:{固定长度值}

范围长度:{最小长度值,最大长度值}

[a-z]{5}:表示内容范围为小写字母a到z且长度必须为5

[a-z]{2,8}:表示内容范围为小写字母a到z且长度在2到8之间,包含2与8

[a-z]{2,}:表示内容范围为小写字母a到z且最小长度为2,最大长度无限制

[a-z]{0,2}:表示内容范围为小写字母a到z且最小长度为0,最大长度为2

public class RegexTest {
	public static void main(String[] args) {
		String regex ="[^ab]{2}";
		String content = "c";   //返回false
		String content1 = "cc";   //返回true
		String content2 = "ccc";   //返回true
		System.out.println(content.matches(regex));
		System.out.println(content1.matches(regex));
		System.out.println(content2.matches(regex));
		//其他的自己试吧
	}

}

3.长度限定符号

长度限定符号是指通过预定义符号来完成长度限定。

?:零次或者一次,等同于{0,1}

+:一次或多次,等同于{1,}

*:零次或多次,等同于{0,}

public class RegexTest {
	public static void main(String[] args) {
		String regex ="[^ab]?";
		String content = "c";   //返回true
		String content1 = "cc";   //返回false
		String content2 = "ccc";   //返回false
		System.out.println(content.matches(regex));
		System.out.println(content1.matches(regex));
		System.out.println(content2.matches(regex));
		//其他的自己试吧
	}

}

4.预定义字符

正则表达式中可以通过一些预定义字符来表示内容限定。目的是为了简化内容限定的定义。

常见的预定义字符

字符描述
\d匹配一个数字字符,等价于[0-9]
\D匹配一个非数字字符,等价于[(^)0-9],没有括号,只是因为md特殊格式。
\n匹配一个换行符
\r匹配一个回车符
\s匹配任何空白字符,包括空格、制表符、换页符等等
\S匹配任何非空白字符
\t匹配一个制表符
\w匹配包括下划线的任何单词字符,等价于[A-Za-z0-9_]
\W匹配任何非单词字符,等价于[(^)A-Za-z0-9_],没有括号,理由同上
public class RegexTest {
	public static void main(String[] args) {
		//String regex ="[^ab]?";
		String regex = "\\d";  //多加一个\表示转义
		String regex1 = "\\d*";  //中间不要加空格
		String regex2 = "\\d{0,1}";
		String content = "1";   //返回true
		String content1 = "11";   //返回false
		String content2 = "c";   //返回false
		System.out.println(content.matches(regex));
		System.out.println(content1.matches(regex));
		System.out.println(content2.matches(regex));
		//其他的自己试吧
	}

}

5.正则表达式的组合定义

在正则表达式中可以通过多个内容限定与长度限定来组合定义

示例:

必须是以字母开头,最少长度为4,最大长度为8

"[a-z]{1}\\w{3,8}"

public class RegexTest {
	public static void main(String[] args) {
		//String regex ="[^ab]?";
		String regex = "[a-z]{1}\\w{3,8}";
		//String regex1 = "\\d*";  //中间不要加空格
		//String regex2 = "\\d{0,1}";
		String content = "1";   //返回false
		String content1 = "11111";   //返回false
		String content2 = "cddd";   //返回true
		System.out.println(content.matches(regex));
		System.out.println(content1.matches(regex));
		System.out.println(content2.matches(regex));
		//其他的自己试吧
	}
}

校验带有区号的电话号码的正则表达式

"\\d{3,4}-\\d{7,8}"

"(\\d{3,4})-(\\d{7,8})"

public class RegexTest {
	public static void main(String[] args) {
		//String regex ="[^ab]?";
		//String regex = "[a-z]{1}\\w{3,8}";
		//String regex1 = "\\d*";  //中间不要加空格
		//String regex2 = "\\d{0,1}";
		String regex = "(\\d{3,4})-(\\d{7,8})";
		String content = "111-1234567";   //返回true
		String content1 = "11-1234567";   //返回false
		String content2 = "123-123456789";   //返回false
		System.out.println(content.matches(regex));
		System.out.println(content1.matches(regex));
		System.out.println(content2.matches(regex));
		//其他的自己试吧
	}
}

6.常见的正则表达式

用户名正则表达式
邮箱([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}
IP地址(25[0-5]|2[0-4]\\d[0-1]\\d{2}|[1-9]?\\d)
URLhttp(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?
身份证号码^\\d{18}$|^\\d{15}$

这些玩意啊,说实在的咱也看不懂,用到了现查就行,也背不下来是不。当然,正则表达式中|表示或,.表示任意字符。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值