正则表达式

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 */

/**
 * @author __LcukyStar
 * 正则表达式
 */
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 一个点代表一个字符。
		print("abc".matches("...")); // true
		// \d代表数字,*代表0个或多个。
		print("123".matches("\\d*")); // true
		// [a,z]代表在字符a~z之中的一个,+表示一个或多个
		print("abc".matches("[a-z]+")); // true
		// ?表示0个或1个
		print("a".matches("[a-z]?")); // true
		// [a-z[A-Z]]表示所有英文字母,并集的意思了。
		print("abcD".matches("[a-z[A-Z]]*")); // true
		
		//////////////////////////////////////////////////////
		//[a-z][a-b]API说是并集,怎么感觉不对。应该是交集吧
		print("abc".matches("[a-z][a-b]*"));// false
		print("abc".matches("[a-z][a-d]*")); // true
		/////////////////////////////////////////////////////
		
		// {m,n}表示前面的字符出现m到n次,即>=m && <=n
		print("abc".matches("[a-c]{1,3}")); // true
		// {m}表示前面的字符出现m次
		print("abc".matches("[a-d]{3}")); // true
		// {m,}表示前面的字符出现的次数>=m
		print("abc".matches("[a-d]{2,}")); // true
		// 或,a,b或c,d之一
		print("abcd".matches("[[a-b]|[c-d]]*")); // true
		// a-d并且a-c出现一次或多次
		print("abc".matches("[[a-d]&&[a-c]]+")); // true
		// a-c的字符出现3次,D-F的字符出现3次,g,h,i字符之一出现3次,后跟一个数字
		print("abcDEFghi0".matches("[a-c]{3}[D-F]{3}[g,h,i]{3}\\d")); // true
		// \D:非数字,等同于[^0-9]
		print("abd~!@#$%^^&".matches("\\D*")); // true
		// ^在[]表示,除了xx,如下面就是除了d之外
		print("abc".matches("[^d]+")); // true
		// 除了c-f之外的字符0个或多个。
		print("abc".matches("[^c-f]*")); // false
		// 以a开头,后跟0到多个字符并且以c结尾
		print("abc".matches("^a[a-z]*c{1}quot;)); // true
		// \s:空白字符[ \t\n\x0B\f\r]
		print(" \n\r\t\f".matches("\\s+")); // true
		// 非空白在字符
		print("abcEDF123!@#~".matches("\\S*")); // true
		// \w:单词字符:[a-zA-Z_0-9]
		print("abc_DEF_0_23445".matches("\\w+")); // true
		// \W:非单词字符:[^\w]
		print("~!@#@%@#%@#%".matches("\\W+")); // true
		print("hello world".matches("z{0}"));
		print("------------------华丽分割线-----------------");
		print("hello 168".matches(".*\\b.*")); // true
		print("hello 168".matches(".*.*")); // true
		print("hello168".matches(".*\\b.*")); // true
		print("------------------华丽分割线-----------------");
		print("aaa@qq.com".matches("\\w[.-]+\\.\\w[.-]+"));
		print("------------------华丽分割线-----------------");
		
		Pattern p = Pattern.compile("\\d{2,3}");
		String str = "12-234-5678";
		Matcher m = p.matcher(str);
		// matches()方法匹配整个字符串
		print(m.matches()); // 是否匹配给定的正则表达式
		//m.reset(); // matches()方法在12-时就返回,因为它匹配的是整个字符串,但是下次会从这个位置开始,因此用reset()方法将其重置。
		print("------------------华丽分割线-----------------");
		print(m.find()); // true,找到234
		print(m.find()); // true,找到567
		print(m.find()); // false
		print(m.find()); // false
		print(m.find()); // false
		print("------------------华丽分割线-----------------");
		// 从开始位置找,找到了就返回true
		print(m.lookingAt()); // true
		print(m.lookingAt()); // true
		print(m.lookingAt()); // true
		print(m.lookingAt()); // true

	}
	public static void print(Object obj) {
		System.out.println(obj);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值