首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-09-14 
通俗JAVA语言中正则表达式的应用Pattern与Matcher做了几年的开发了,就只会在java中用str.matches(expStr),

通俗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

?

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

热点排行