正则表达式

                   RegularExpressions:用途——字符串匹配(字符匹配)、字符串查找、字符串替换;例如:IP地址是否正确、从网页中揪出Email、从网页中揪出链接;类——java.lang.String、java.util.regex.Pattern、java.util.regex.Matcher
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
	public static void main(String arg[]) {
		// 简单认识正则表达式的概念
       
		p("abc".matches("..."));  //字符串"abc"符合"..."这种模式
		p("a8729a".replaceAll("\\d", "-")); //将所有的数字转换为'-',转义字符'\'
		Pattern p = Pattern.compile("[a-z]{3}"); //编译正则表达式,表示由字符a-z组成的3个字符的字符串
		Matcher m = p.matcher("fgh");  //进行匹配,结果保留在Matcher的对象m中
		p(m.matches());
		p("fgha".matches("[a-z]{3}"));  //将上面3语句综合
		
		
		//初步认识. * + ?
		
		p("aa".matches("..")); //.代表一个
		p("aa".matches("aa"));
		p("aaaa".matches("a*"));//*代表0个或多个
		p("aaaa".matches("a+"));  //+代表一个或多个
		p("".matches("a*"));
		p("aaaa".matches("a?")); //?代表一个或0个
		p("".matches("a?"));  //零宽度匹配
		p("a".matches("a?"));
		p("214523145234532".matches("\\d{3,100}")); //{}代表数字出现至少3次,至多100次(3到100为)
		p("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
		p("192".matches("[0-2][0-9][0-9]")); //[]表示在范围内
		
		
		//范围
		
		p("a".matches("[abc]")); //abc中取一个
		p("a".matches("[^abc]")); //取除了a
		p("A".matches("[a-zA-Z]")); // 取a-z 或A-Z
		p("A".matches("[a-z]|[A-Z]"));  //取a-z 或A-Z
		p("A".matches("[a-z[A-Z]]"));  // 取a-z 或A-Z
		p("R".matches("[A-Z&&[RFG]]"));  // 取交集
		
		//认识\s() \w(a-zA-Z_0-9) \d(一位数字) \D(非数字字符) \()
		
		p(" \n\r\t".matches("\\s{4}"));
		p(" ".matches("\\S"));
		p("a_8".matches("\\w{3}"));
		p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));
		p("\\".matches("\\\\"));
		
		//POSIX Style
		p("a".matches("\\p{Lower}"));
		
		//boundary 开始和结尾
		
		p("hello sir".matches("^h.*"));  //以h开头
		p("hello sir".matches(".*ir$"));
		p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
		p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
		//whilte lines
		p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));
		
		//email
		p("[email protected]".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
		
		//matches find lookingAt
		
		Pattern p = Pattern.compile("\\d{3,5}"); //3-5位的数数字字符串
		String s = "123-34345-234-00";
		Matcher m = p.matcher(s);   //matcher()匹配整个字符串,停留在不匹配处
		p(m.matches());
		m.reset();   //reset 重新设置0处为开始查找的位置
		p(m.find());    //找字串Pattern.compile("\\d{3,5}");部分,找到则去掉
		p(m.start() + "-" + m.end());
		p(m.find());
		p(m.start() + "-" + m.end()); //起始结束位置
		p(m.find());
		p(m.start() + "-" + m.end());
		p(m.find());
		//p(m.start() + "-" + m.end());
		p(m.lookingAt());    //从开始位置开始查找
		p(m.lookingAt());
		p(m.lookingAt());
		p(m.lookingAt());
		
		//replacement 替换
		
		Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); //忽略大小写
		Matcher m = p.matcher("java Java adadas JAVa JaVa IloveJAVA you hateJava afasdfasdf");
		StringBuffer buf = new StringBuffer();
		int i=0;
		while(m.find()) { //查找Pattern.compile部分
			i++;
			if(i%2 == 0) {
				m.appendReplacement(buf, "java");  //替换
			} else {
				m.appendReplacement(buf, "JAVA");
			}
		}
		m.appendTail(buf); //添加尾巴,结尾部分
		p(buf);
		
		//group 分组
		
		Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");//分组用"()"
		String s = "123aa-34345bb-234cc-00";
		Matcher m = p.matcher(s);
		while(m.find()) {
			p(m.group(0)); //打印第一组
			p(m.group(1)); //从左边起按'('分组,第几个就是第几组
			p(m.group(2));
		}
		
		//qulifiers
		
		Pattern p = Pattern.compile(".{3,10}+[0-9]");
		String s = "aaaa5bbbb68";
		Matcher m = p.matcher(s);
		if(m.find())
			p(m.start() + "-" + m.end());
		else 
			p("not match!");
		
		//non-capturing groups  非捕获
		                                         //(?=a)在前面,以a开始3个
		Pattern p = Pattern.compile(".{3}(?=a)");  //3个字符,(?=a)在后面表示a前面3个,以a(不包含a)结束
		String s = "4474a66b";
		Matcher m = p.matcher(s);
		while(m.find()) {
			p(m.group());
		}
		
		//back refenrences   向前引用
		
		Pattern p = Pattern.compile("(\\d(\\d))\\2");
		String s = "122";
		Matcher m = p.matcher(s);
		p(m.matches());
				
		//flags的简写
		//Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); //忽略大小写
		p("Java".matches("(?i)(java)"));  //(?i) 为上面简写
	}

	public static void p(Object o) {
		System.out.println(o);  //默认调用o的toString方法
	}
}

你可能感兴趣的:(java,正则表达式)