Welcome, NewComer

Java正则表达式

基本规则

image

要点

image
image

Test

	//13112345678    1   [3-9]   \\d{9}
	String regex1 = "1[3-9]\\d{9}";
	System.out.println("13154654456".matches(regex1));//true
	System.out.println("03154654456".matches(regex1));//false
	System.out.println("12154654456".matches(regex1));//false
	System.out.println("1315454456".matches(regex1));//false
	System.out.println("------------------------------------------");

	//0317-4261090 020-2325515
	//区号:   0   \\d{2,3}
	//-  出现0或1次:    -?
	//号码:   [1-9]   \\d{4,9}
	String regex2 = "0\\d{2,3}-?[1-9]\\d{4,9}";
	System.out.println("0317-4264545".matches(regex2));//true
	System.out.println("0174264545".matches(regex2));//true
	System.out.println("1317-4264545".matches(regex2));//false
	System.out.println("017-445".matches(regex2));//false
	System.out.println("0317-0264545".matches(regex2));//false
	System.out.println("------------------------------------------");

	//2085086966@qq.com asdf546a@pci.com.cn
	//@前:任意数字字母下划线至少出现一次 \\w+
	//@到.:任意数字字母出现少于6次[\\w&&[^_]]{2,6}
	//(.   [a-zA-Z]{2,3})   {1,2}:.com/.cn等为一组,出现一到两次
	String regex3 = "\\w+@[\\w&&[^_]]{2,6}(.[a-zA-Z]{2,3}){1,2}";

	System.out.println("2085086966@qq.com".matches(regex3));//true
	System.out.println("2085086966@pci.com.cn".matches(regex3));//true
	System.out.println("2085086966@qq.cn".matches(regex3));//true
	System.out.println("20850_adas@qq.com".matches(regex3));//true
	System.out.println("------------------------------------------");

	//大小写数字下划线4-16位
	String regex4 = "\\w{4,16}";

	//身份证号码简单验证
	String regex5 = "[1-9]\\d{16}(\\d|X|x)";

	//身份证号码简验证
	String regex6 = "[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[12])(0[1-9]|[12][0-9]|3[01])\\d{3}(\\d|X|x)";

	//忽略大小写
	String regex7 = "a(?i)bc";
	System.out.println("aBC".matches(regex7));//true
	System.out.println("ABC".matches(regex7));//false

插件

  • any_rule

捕获分组和非捕获分组


posted @ 2024-07-30 18:25  Through287  阅读(24)  评论(0)    收藏  举报