新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何使用Java正則表達式驗證固定電話號碼符合性
這篇文章主要介紹如何使用Java正則表達式驗證固定電話號碼符合性,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
10年積累的網(wǎng)站設計制作、做網(wǎng)站經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先建設網(wǎng)站后付款的網(wǎng)站建設流程,更有葫蘆島免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
具體代碼如下所示:
/** * 驗證固定電話號碼的合法性 * @author jy */ package phone; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneTest { public static boolean isPhone(String str) { Pattern p1 = null, p2 = null; Matcher m = null; boolean isPhone = false; p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 驗證帶區(qū)號的 p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 驗證沒有區(qū)號的 if (str.length() > 9) { m = p1.matcher(str); isPhone = m.matches(); } else { m = p2.matcher(str); isPhone = m.matches(); } return isPhone; } public static void main(String[] args) { String phone = "0770-88889999"; if(isPhone(phone)){ System.out.println(phone+"是符合的電話號碼"); }else { System.out.println(phone+"不符合"); } } }
下面看下用正則表達式判斷一個字符串是否全是數(shù)字
用正則表達式首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
public boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if( !isNum.matches() ){ return false; } return true; }
以上是“如何使用Java正則表達式驗證固定電話號碼符合性”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:如何使用Java正則表達式驗證固定電話號碼符合性
轉(zhuǎn)載來源:http://fisionsoft.com.cn/article/giedgs.html