前端之家收集整理的这篇文章主要介绍了
正则表达式提取电话号码,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
android中在字符串中提取电话号码
package app.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList();
list=getNumber2("我的电话是13146921596,固定电话是010-12345678");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
public static ArrayList list;
public static String getNumber(String content) {
list = new ArrayList();
Pattern p = Pattern.compile("\\d{3}-\\d{8}|\\d{4}-\\d{7}|\\d{11}");
Matcher matcher = p.matcher(content);
if (matcher.find()) {
String n = matcher.group(0);
return n;
}
return null;
}
public static ArrayList getNumber2(String content) {
list = new ArrayList();
Pattern p = Pattern.compile("\\d{3}-\\d{8}|\\d{4}-\\d{7}|\\d{11}");
Matcher matcher = p.matcher(content);
while (matcher.find()) {
String n = matcher.group(0).toString();
list.add(n);
}
return list;
}
}