正则表达式

仅供个人学习,如有侵权请联系删除。

正则表达式是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
            * 作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的
        * 案例演示
            * 需求:校验qq号码.
                * 1:要求必须是5-15位数字
                * 2:0不能开头
                * 3:必须都是数字

        String regex = "[1-9]\\d{4,14}";
		System.out.println("2553868".matches(regex));
		System.out.println("012345".matches(regex));
		System.out.println("2553868abc".matches(regex));

 

        [abc] a、b 或 c(简单类) 
        [^abc] 任何字符,除了 a、b 或 c(否定) 
        [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围) 
        [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集) 
        [a-z&&[def]] d、e 或 f(交集) 
        [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去) 
        [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去) 
        []    代表的是单个字符,如果用一个字符串(多个字符)用该正则表达式,则一定是false。

private static void demo1() {
		String regex = "[abc]";					//[]代表单个字符
		System.out.println("a".matches(regex));
		System.out.println("b".matches(regex));
		System.out.println("c".matches(regex));
		System.out.println("d".matches(regex));
		System.out.println("1".matches(regex));
		System.out.println("%".matches(regex));
		System.out.println("ab".matches(regex));
	}
/*
true
true
true
false
false
false
false
*/

private static void demo2() {
		String regex = "[a-d[m-p]]";
		System.out.println("a".matches(regex));
		System.out.println("m".matches(regex));
		System.out.println("n".matches(regex));
        System.out.println("e".matches(regex));
	}
/*
true
true
true
false
*/
private static void demo5() {
		String regex = "[a-z&&[def]]";						//取交集
		System.out.println("a".matches(regex));
		System.out.println("d".matches(regex));
	}
/*
false
true
*/

 

         . 任何字符
        \d 数字:[0-9] 
        \D 非数字: [^0-9] 
        \s 空白字符:[ \t\n\x0B\f\r] 
        \S 非空白字符:[^\s] 
        \w 单词字符:[a-zA-Z_0-9] (a-z,A-Z,0-9,_)
        \W 非单词字符:[^\w] 

private static void demo1() {
		String regex = "..";
		System.out.println("a".matches(regex));
		System.out.println("ab".matches(regex));
	}
/*
false
true
*/

private static void demo2() {
		String regex = "\\d";					//\代表转义字符,如果想表示\d的话,需要\\d
		System.out.println("0".matches(regex));
		System.out.println("a".matches(regex));
		System.out.println("9".matches(regex));
	}
/*
true
false
true
*/

private static void demo3() {
		String regex = "\\s";
		System.out.println(" ".matches(regex));                //一个空格
		System.out.println("	".matches(regex)); 				//一个tab键
		System.out.println("    ".matches(regex)); 				//四个空格
	}
/*
true
true
false
*/

private static void demo4() {
		String regex = "\\w";
		System.out.println("a".matches(regex));
		System.out.println("z".matches(regex));
		System.out.println("_".matches(regex));
		System.out.println("%".matches(regex));
	}
/*
true
true
true
false
*/

 

        X?   零次或一次
        X*   零次到多次 
        X+   一次或多次 
        X{n}   恰好 n 次 
        X{n,}   至少 n 次 
        X{n,m}   至少 n 次,但是不超过 m 次 

public static void demo1() {
		String regex = "[abc]?";
		System.out.println("a".matches(regex));
		System.out.println("b".matches(regex));
		System.out.println("c".matches(regex));
		System.out.println("d".matches(regex));
		System.out.println("".matches(regex));
	}
/*
true
true
true
false
true
*/

public static void demo2() {
		String regex = "[abc]*";
		System.out.println("".matches(regex));
		System.out.println("abc".matches(regex));
		System.out.println("a".matches(regex));
	}
/*
true
true
true
*/

public static void demo3() {
		String regex = "[abc]+";
		System.out.println("".matches(regex));
		System.out.println("a".matches(regex));
		System.out.println("aaaaabbbbccccc".matches(regex));
	}
/*
false
true
true
*/

private static void demo4() {
		String regex = "[abc]{5,15}";
		System.out.println("abcba".matches(regex));
		System.out.println("abcbaabcabbabab".matches(regex));
		System.out.println("abcb".matches(regex));
		System.out.println("abcbaabaabcbaaba".matches(regex));
	}
/*
true
true
false
false
*/

 

String类中的split(String regex)方法,在切割位置切割一个字符串为字符数组

public static void main(String[] args) {
		String s = "one.two.three";
	//	String[] arr = s.split(".");				必须用转义字符才行,否则函数认为"."是任何字符都可匹配成功,而非认为是单独的字符"."
		String[] arr = s.split("\\.");				//通过正则表达式切割字符串
		System.out.println("1".matches("."));
		System.out.println("!".matches("."));
		System.out.println("~".matches("."));
		System.out.println("a".matches("."));
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}
/*
true
true
true
true
one
two
three
*/

 

String类的功能:replaceAll(String regex,String replacement)

public static void main(String[] args) {
		String s = "wo111ai222heima";
		String regex = "\\d";			//\\d代表的是任意数字
		String s2 = s.replaceAll(regex, "");
		System.out.println(s2);
	}
/*
woaiheima
*/

正则表达式的分组功能
            * 捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B(C))) 中,存在四个这样的组: 
        
                1     ((A)(B(C))) 
                2     (A 
                3     (B(C)) 
                4     (C) 
            
                组零始终代表整个表达式。

private static void demo3() {
    /*
	 *  需求:我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程
		将字符串还原成:“我要学编程”。
	 */
		String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
		String s2 = s.replaceAll("\\.+", "");
		String s3 = s2.replaceAll("(.)\\1+", "$1");	//$1代表第一组中的内容
		System.out.println(s3);
	}

	public static void demo2() {
		//需求:请按照叠词切割: "sdqqfgkkkhjppppkl";
		//结果:sd fg hj kl
		String s = "sdqqfgkkkhjppppkl";
		String regex = "(.)\\1+";					//+代表第一组出现一次到多次
		String[] arr = s.split(regex);
		
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}

	public static void demo1() {
		//叠词 快快乐乐,高高兴兴
		/*String regex = "(.)\\1(.)\\2";					//\\1代表第一组又出现一次	\\2代表第二组又出现一次
		System.out.println("快快乐乐".matches(regex));
		System.out.println("快乐乐乐".matches(regex));
		System.out.println("高高兴兴".matches(regex));
		System.out.println("死啦死啦".matches(regex));
true false true false        
*/
		
		//叠词 死啦死啦,高兴高兴
		String regex2 = "(..)\\1";
		System.out.println("死啦死啦".matches(regex2));
		System.out.println("高兴高兴".matches(regex2));
		System.out.println("快快乐乐".matches(regex2));
        /*
        true
        true
        false
        */
	}

 

        正则表达式的获取功能
            * Pattern和Matcher的结合使用
        案例演示
            * 需求:把一个字符串中的手机号码获取出来

public static void main(String[] args) {
		//demo1();
		String s = "我的手机是18511866260,我曾用过18987654321,还用过18812345678";
		String regex = "1[3578]\\d{9}";        //第一位是1.第二位是{3,5,7,8},后九位任意数字,一共11位数字
		
		
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(s);
		
		/*boolean b1 = m.find();
		System.out.println(b1);
		System.out.println(m.group());
		
		boolean b2 = m.find();
		System.out.println(b2);
		System.out.println(m.group());*/
		
		while(m.find())
			System.out.println(m.group());   //字符串从前到后寻找符合正则表达式的序列,每找一次,指针指向符合的序列,再次寻找,指针指向下一个符合的序列。find和group不能分开,必须先找再获取。
	}

	public static void demo1() {
		Pattern p = Pattern.compile("a*b");					//获取到正则表达式
		Matcher m = p.matcher("aaaaab");					//获取匹配器
		boolean b = m.matches();							//看是否能匹配,匹配就返回true
		
		System.out.println(b);
		
		System.out.println("aaaaab".matches("a*b"));  		//与用Pattern和Matcher类结果一样
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值