private static final Pattern MOBILE_PATTERN = Pattern.compile(
"^(?:(?:\\+86|86)?1(?:3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-9])\\d{8})$"
);
| 前綴 | 運營商/類型 | 範例 |
|---|---|---|
| 13x | 移動/聯通/電信 | 130-139 |
| 14[5-9] | 衛星/物聯網 | 145-149 |
| 15[0-35-9] | 移動/聯通/電信(除 154) | 150-153,155-159 |
| 16[2567] | 電信/聯通 5G/物聯網 | 162,165-167 |
| 17[0-8] | 虛擬運營商/物聯網 | 170-178 |
| 18x | 移動/聯通/電信 | 180-189 |
| 19[0-9] | 5G 新號段 | 190-199 |
public static boolean isMobile(String mobile) {
if (mobile == null) return false;
return MOBILE_PATTERN.matcher(mobile).matches();
}
// 允許空格/分隔符的寬鬆版
public static boolean isMobileLoose(String mobile) {
if (mobile == null) return false;
String clean = mobile.replaceAll("[\\s-]", "");
return MOBILE_PATTERN.matcher(clean).matches();
}
Pattern.compile 只執行一次,避免重複編譯開銷(?:...) 減少分組開銷String[] tests = {
"13800138000", "15912345678", "19988888888", // 有效
"12800138000", "1380013800", "138-0013-8000", // 無效/需清洗
"+8613800138000", "8613800138000" // 含國碼
};
for (String t : tests) System.out.println(t + " -> " + isMobile(t));
生產環境建議定期從 工信部 或三大運營商公開接口拉取最新號段,動態生成正則或使用 Trie 樹匹配,確保 100% 覆蓋。