matches 正则表达式的使用 编写代码,验证手机号码,并告知用户是"189""158""130" 号段各自的运营公司, 如果号码长度有误,告诉用户手机号码 不存在。

来源:互联网 发布:mac白屏无法进入系统 编辑:程序博客网 时间:2024/05/14 09:56

public class StringTest {
 /**
  * 编写代码,验证手机号码,并告知用户是"189""158""130" 号段各自的运营公司, 如果号码长度有误,告诉用户手机号码 不存在。
  * 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
  * 联通:130、131、132、152、155、156、185、186
  * 电信:133、153、180、189、(1349卫通)
  */
 public void CheckCellphone(String input) {
  String regex1 = "1(((3[5-9]|5[017-9]|8[78])[//d]{8})|(34[^9][//d]{7}))";
  String regex2 = "1(3[0-2]|5[256]|8[56])[//d]{8}";
  String regex3 = "1(((33|53|8[09])[//d]{8})|(349[//d]{7}))";
  boolean b1 = input.matches(regex1);
  boolean b2 = input.matches(regex2);
  boolean b3 = input.matches(regex3);
  if (b1) {
   System.out.println("该手机号码的运营公司是移动!");
  } else if (b2) {
   System.out.println("该手机号码的运营公司是联通!");
  } else if (b3) {
   System.out.println("该手机号码的运营公司是电信!");
  } else {
   System.out.println("该手机号码 不存在!");
  }
 }

 public static void main(String[] args) {
  st.CheckCellphone("13492222222");
 }

}

原创粉丝点击