Pattern p = Pattern.compile("[0-9]{7,8}");
String str = "11223333 2233311 22231233";
Matcher m = p.matcher(str);
//由于不知道有多少个电话号码会被匹配出来,所以采用List存放电话号码
List<String> mobileList = new ArrayList<String>();
while(m.find()){
mobileList.add(m.group()); //将匹配出的电话号码存放到mobileList中
}
String[] mobiles = new String[mobileList.size()]; //声明数组存放电话号码
int i = 0;
for(String mobile : mobileList){ //将list中的电话号码转存到数组中
mobiles[i] = mobile;
i++;
}
for(int j=0;j<mobiles.length;j++){
System.out.println(mobiles[j]); //打印电话号码
}
温馨提示:内容为网友见解,仅供参考