java Pattern类

 

string类中的matches方法:

public boolean matches(String regex) {
return Pattern.matches(regex, this);
}

 

 

思考题:

/* 需求:把一个字符串中的手机号码获取出来*/

public static void main(String[] args) {
//demo1();
String s = "我的手机是18988888888,我曾用过18987654321,还用过18812345678";
String regex = "1[3578]\\d{9}";


Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);

/*boolean b1 = m.find();
System.out.println(b1);
System.out.println(m.group());

boolean b2 = m.find();
System.out.println(b2);
System.out.println(m.group());*/

while(m.find())
System.out.println(m.group());

posted @ 2017-03-03 23:39  yimian  阅读(723)  评论(0编辑  收藏  举报
访问人数:AmazingCounters.com