Java学习笔记之正则表达式(一):正则表达式的规则

package com.collection.regex;

import org.junit.Test;

/*
正则表达式:正则表达式其实就是用于操作字符串的一个规则,使用特殊的符号表示;

需求:
	校验一个QQ号:
	1、不能以0开头;
	2、长度为5~12位;
	3、只能由数字组成;

 */

public class Demo1 {
	
//	String qq = "123";
//	String qq = "01234567";
//	String qq = "123abc4567";
	String qq = "1234567";
	
	// 该方法可以实现,但是太复杂,太麻烦
	@Test
	public void test1(){

		if (!qq.startsWith("0")){
			if (qq.length() >= 5 && qq.length() <= 12){
				try {
					Long.parseLong(qq);
					System.out.println("合法QQ");
				} catch (Exception e) {
					System.out.println("非法QQ,QQ只能由数字组成");
				}
			}else{
				System.out.println("非法QQ,QQ长度必须为5~12位");
			}
		}else{
			System.out.println("非法QQ,QQ不能以0开头");
		}
	}
	
	// 使用正则表达式进行校验
	@Test
	public void test2(){
		System.out.println(qq.matches("[1-9]\\d{4,11}")?"合法QQ":"非法QQ");
	}
	
}


package com.collection.regex;

import org.junit.Test;

/*
 * 正则表达式规则:
 */

public class Demo2 {

	/*
	预定义字符类:
		.	任意字符
		\d	数字:[0-9]
		\D	非数字:[^0-9]
		\s	空白字符:[\t\n\x0B\f\r]
		\S	非空白字符:[^\s]
		\w	单词字符:[a-zA-z_0-9]
		\W	非单词字符:[^\w]
		
		注意:任何预定义字符在没有加上数量词之前,都只能匹配一个字符而已;
	 */
	
	@Test
	public void test1(){
		System.out.println("------------ . -------------");
		
		// . 任意字符:只能匹配单个字符
		System.out.println("1".matches("."));	// true
		System.out.println("a".matches("."));	// true
		System.out.println("%".matches("."));	// true
		System.out.println("-".matches("."));	// true
		System.out.println("\\".matches("."));	// true(两个\\表示一个\,前一个是转义字符)
		System.out.println("\r".matches("."));	// false(只能匹配单个字符)
		System.out.println("haha".matches("."));// false(只能匹配单个字符)
		
		System.out.println("------------ \\d --------------");
		
		// \d 表示数字[0-9];要想\d表示自己所表达的意思,需要加一个转义字符(\),即写成 \\d;
		System.out.println("0".matches("\\d"));	// true
		System.out.println("8".matches("\\d"));	// true
		System.out.println("82".matches("\\d"));// false(只能匹配单个字符)
		System.out.println("a".matches("\\d"));	// false

		System.out.println("------------- \\D -------------");
		
		// \D 表示非数字^[0-9];只要不是数字,其他任何字符都可以;
		System.out.println("2".matches("\\D"));	// false
		System.out.println("a".matches("\\D"));	// true
		System.out.println("@".matches("\\D"));	// true
		System.out.println("+".matches("\\D"));	// true

		System.out.println("------------- \\s -------------");
		
		// \s 空白字符:[\t\r\n\x0B\f]
		System.out.println("\r".matches("\\s"));// true
		System.out.println("\n".matches("\\s"));// true
		System.out.println("\t".matches("\\s"));// true
		System.out.println("\f".matches("\\s"));// true
		System.out.println(" ".matches("\\s"));	// true
		System.out.println("a".matches("\\s"));	// false

		System.out.println("------------- \\S -------------");
		
		// \S 非空白字符:^[\s]
		System.out.println(" ".matches("\\S"));	// false
		System.out.println("a".matches("\\S"));	// true
		System.out.println("^".matches("\\S"));	// true

		System.out.println("------------- \\w -------------");
		
		// \w 单词字符:[a-zA-z_0-9]
		System.out.println("w".matches("\\w"));	// true(小写字母可以)	
		System.out.println("R".matches("\\w"));	// true(大写字母可以)
		System.out.println("_".matches("\\w"));	// true(下划线可以)
		System.out.println("7".matches("\\w"));	// true(数字可以)
		System.out.println("@".matches("\\w"));	// false(特殊字符不行)
		System.out.println("-".matches("\\w"));	// false(运算符号不行)

		System.out.println("------------- \\W -------------");
		
		// \W 非单词字符:^[\w]
		System.out.println("w".matches("\\W"));	// false	
		System.out.println("R".matches("\\W"));	// false
		System.out.println("_".matches("\\W"));	// false
		System.out.println("7".matches("\\W"));	// false
		System.out.println("@".matches("\\W"));	// true
		System.out.println("-".matches("\\W"));	// true
	}
	
	/*
	数量词:写在{}里面(和范围词区分);
		?		0次或1次
		*		0次或多次
		+		1次或多次
		{n}		正好n次
		{n,}	至少n次
		{n,m}	至少n次,但不超过m次(n-m次)
	 */
	@Test
	public void test2(){
		System.out.println("--------- ? ---------");
		
		// \\d? :出现0次或1次任意数字
		System.out.println("".matches("\\d?"));		// true
		System.out.println("2".matches("\\d?"));	// true
		System.out.println("28".matches("\\d?"));	// false
		System.out.println(" ".matches("\\d?"));	// false
		System.out.println("a".matches("\\d?"));	// false

		System.out.println("--------- * ---------");

		// \\d* :出现0次或多次任意数字
		System.out.println("".matches("\\d*"));		// true
		System.out.println("3".matches("\\d*"));	// true
		System.out.println("1234".matches("\\d*"));	// true
		System.out.println("&".matches("\\d*"));	// false

		System.out.println("--------- + ---------");
		
		// \\d+ :出现1次或多次任意数字
		System.out.println("".matches("\\d+"));		// false
		System.out.println("32".matches("\\d+"));	// true
		System.out.println("9876543210".matches("\\d+"));	// true

		System.out.println("--------- {n} ---------");
		
		// \\d{6} :只能出现6位任意数字
		System.out.println("123456".matches("\\d{6}"));	// true
		System.out.println("123".matches("\\d{6}"));	// false

		System.out.println("--------- {n,} ---------");
		
		// \\d{3,} :至少出现3位任意数字
		System.out.println("22".matches("\\d{3,}"));	// false
		System.out.println("223".matches("\\d{3,}"));	// true
		System.out.println("13241".matches("\\d{3,}"));	// true

		System.out.println("--------- {n,m} ---------");
		
		// \\d{3,6} :出现3-6位任意数字
		System.out.println("12".matches("\\d{3,6}"));	// false
		System.out.println("123".matches("\\d{3,6}"));	// true
		System.out.println("123456".matches("\\d{3,6}")); // true
		System.out.println("1234567".matches("\\d{3,6}")); // false
	}
	
	/*
	范围词:写在中括号[]内;数量词写在大括号{}内;
		[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;可以直接写成[def](交集);
		
		注意:范围词里面不管有多长,没有数量词的配合,都只能匹配一个字符;
	 */
	@Test
	public void test3(){
		System.out.println("------- [abc] ------");
		
		// [abc] :出现abc中的其中一个字符;只能是一个字符;
		System.out.println("abc".matches("[abc]"));	// false
		System.out.println("ab".matches("[abc]"));	// false
		System.out.println("a".matches("[abc]"));	// true
		System.out.println("d".matches("[abc]"));	// false

		System.out.println("------- [^abc] ------");
		
		// [^abc] :除了a、b或c之外的任何字符,否定词;
		System.out.println("a".matches("[^abc]"));	// false
		System.out.println("&".matches("[^abc]"));	// true

		System.out.println("------- [a-zA-Z] ------");
		
		// [a-zA-Z] :26个字母中任意一个都可以,包括大小写
		System.out.println("w".matches("[a-zA-Z]"));// true
		System.out.println("Z".matches("[a-zA-Z]"));// true
		System.out.println("2".matches("[a-zA-Z]"));// false
		System.out.println("&".matches("[a-zA-Z]"));// false
	}
}

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值