直接上代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result_actvity);
init();
String str="n13977777777s18911111111你好15988888888hha0955-7777777sss0775-6678111";
checkCellphone(str);
checkTelephone(str);
}
public static void checkCellphone(String str){
Pattern pattern = Pattern.compile("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println("查询到一个符合的手机号码:"+matcher.group());
Log.e("aaaaaa", "checkCellphone: "+matcher.group());
}
}
public static void checkTelephone(String str){
Pattern pattern = Pattern.compile("(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println("查询到一个符合的固定号码:"+matcher.group());
}
}
2021-10-26 14:59:26.888 7839-7839/com.example.recyclerdemo E/aaaaaa: 查询到一个符合的手机号码: 13977777777
2021-10-26 14:59:26.888 7839-7839/com.example.recyclerdemo E/aaaaaa: 查询到一个符合的手机号码: 18911111111
2021-10-26 14:59:26.888 7839-7839/com.example.recyclerdemo E/aaaaaa: 查询到一个符合的手机号码: 15988888888
2021-10-26 14:59:26.889 7839-7839/com.example.recyclerdemo E/aaaaaa: 查询到一个符合的固定号码: 0955-7777777
2021-10-26 14:59:26.889 7839-7839/com.example.recyclerdemo E/aaaaaa: 查询到一个符合的固定号码: 0775-6678111
|