浅显JAVA语言中正则表达式的应用Pattern与Matcher

通俗JAVA语言中正则表达式的应用Pattern与Matcher

做了几年的开发了,就只会在java中用str.matches(expStr),如

String s = "010-12345678";
System.out.println(s.matches("\\d{3}-\\d{8}"));

 

现在想要得到表达式匹配出来的值,要怎么做呢?例如,要得到字符串liu.hi@sina.com中所有连续的字母。

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


public class TestMatch {

	public static void main(String[] args) {
		//例一		
		String s = "010-12345678";
		System.out.println(s.matches("\\d{3}-\\d{8}"));
		//例二		
		Pattern p =Pattern.compile("(\\w+)");
		Matcher m = p.matcher("liu.hi@sina.com");
		while(m.find()){
			System.out.println(m.group());
		}
	}

}

 

输出结果为:

true
liu
hi
sina
com

 

非常小儿科,做这个备忘吧。