正则表达式

package com.reg.sym;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestReg {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * 采用的匹配方式一:次方法是为了有Pattern和Matcher有很多可用的方式
		 */
	/*	Pattern pa = Pattern.compile("[a-z]{3}");
		Matcher m = pa.matcher("ghu");
		boolean ma1=m.matches();
		p(ma1);//true
*/		
		/*
		 * 采用的匹配方式二:一般采用此方法,除非特殊需要采用方式一
		 */
		/*boolean ma2="ghu".matches("[a-z]{3}");
		p(ma2);//true
*/		/*
		 * . * + ?
		 */
		/*p("aaaa".matches("...."));//true
		p("aaaa".matches("a*"));//true
		p("aaaa".matches("a+"));//true
		p("aaaa".matches("a?"));//false
*/		/*
		 * \d 表示数字
		 * 
		 * {a}包含的个数是a个
		 * {a,}包含的个数多于a(含)个
		 * {a,b}包含的个数比a(含)大比b(含)小
		 * 
		 * [abc]就在abc中选择
		 * [^abc]不在abc中选择
		 * [a-zA-Z]在a到z中间或者在A到Z中间
		 * [a-z]|[A-Z]同上
		 * [a-z[A-Z]]同上
		 */
	/*	p("214523145234532".matches("\\d{3,100}"));//true
		p("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));//false
		p("192".matches("[0-2][0-9][0-9]"));//true
		p("a".matches("[abc]"));//true
		p("a".matches("[^abc]"));//false
		p("A".matches("[a-zA-Z]"));//true
		p("A".matches("[a-z]|[A-Z]"));//true
		p("A".matches("[a-z[A-Z]]"));//true
		p("R".matches("[A-Z&&[RFG]]"));//true
		*/
		/*
		 * ^表示开头
		 * $表示结尾
		 * \b表示单词之间有空格
		 */
	/*	p("hello sir".matches("^h.*"));//true
		p("hello sir".matches(".*ir$"));//true
		p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));//true
		p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));//false
*/		/*
         *\d 表示[0-9]
         *\D 表示不是数字
         *\s 表示空白字符[ \n\r\t]
         *\S 表示非空白字符
		 * \w表示为单词的字符[a-zA-Z_0-9]
		 * \W表示为非单词字符
		 * 此处为匹配email
		 */
	/*	p("[email protected]".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));//true
	 * 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("\\\\"));
	 * 
		*/
		/*
		 * find 不是从头开始找
		 * lookingAt 从头开始找
		 * 
		 */
	/*	Pattern p = Pattern.compile("\\d{3,5}");
		String s = "123-34345-234-00";
		Matcher m = p.matcher(s);
		p(m.matches());//false,此时指向到了-这里了
		m.reset();//让程序还是指向s开头,重新恢复初始状态
		p(m.find());//true
		p(m.start() + "-" + m.end());//0-3
		p(m.find());//true
		p(m.start() + "-" + m.end());//4-9
		p(m.find());//true
		p(m.start() + "-" + m.end());//10-13
		p(m.find());//false
		
		p(m.lookingAt());//每一次找的时候都是从头开始
		p(m.lookingAt());
		p(m.lookingAt());
		p(m.lookingAt());
		*/
		/*
		 * 找java这个词并且忽略大小写,奇数为java,偶数为JAVA
		 */
	/*	Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
		StringBuffer buf = new StringBuffer();
		int i=0;
		while(m.find()) {
			i++;
			if(i%2 == 0) {
				m.appendReplacement(buf, "java");
			} else {
				m.appendReplacement(buf, "JAVA");
			}
		}
		m.appendTail(buf);//不写这句就是JAVA java JAVA java IloveJAVA you hatejava
		p(buf);//JAVA java JAVA java IloveJAVA you hatejava afasdfasdf
*/
		/*
		 * ()表示分组 ,次数是数字和字母分组
		 */
		/*
		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());//123aa	34345bb	234cc
		}
	}*/
		/*
		 * Match的方式m.start()和m.end()
		 */
	/*Pattern p = Pattern.compile(".{3,10}[0-9]");//此时打印为:0-10
	   Pattern p = Pattern.compile(".{3,10}?[0-9]");//此时打印为:0-5
	 	Pattern p = Pattern.compile(".{3,10}+[0-9]");//此时打印为:not match
	
		String s = "aaaa5bbb68";
		Matcher m = p.matcher(s);
		if(m.find())
			p(m.start() + "-" + m.end());
		else 
			p("not match!");
		*/
	/*	Pattern p = Pattern.compile(".{3}(?=a)");
		String s = "444a66b";
		Matcher m = p.matcher(s);
		while(m.find()) {
			p(m.group());//444
		}*/
		/*
		 * 
		 */
/*		Pattern p = Pattern.compile("(\\d(\\d))\\2");
		String s = "122";
		Matcher m = p.matcher(s);
		p(m.matches());//true
		p("Java".matches("(?i)(java)"));//true
*/	
/*
		 * flags的简写
		 * 下面两种方法等价
		 */
		Pattern pattern =Pattern.compile("java",Pattern.CASE_INSENSITIVE);
		p("JAva".matches("(?i)(java)"));	
	public static void p(Object o){
		System.out.println(o);
	}

}
	/*
	 * 爬出网页所有邮箱的代码
	 */
	public class EmailSpider {

		public static void main(String[] args) {
			try {
				BufferedReader br = new BufferedReader(new FileReader("D:\\share\\courseware\\1043633.html"));
				String line = "";
				while((line=br.readLine()) != null) {
					parse(line);
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		private static void parse(String line) {
			Pattern p = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
			Matcher m = p.matcher(line);
			while(m.find()) {
				System.out.println(m.group());
			}
		}

	}

}

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