Java String match phone number
public class Main { public static void main(String[] argv) throws Exception { String text = "13012312312"; System.out.println(verifyMobile(text)); }// w w w. ja va 2 s . c o m public static boolean verifyMobile(String text) { boolean flag = false; String reg = "^1[3458][0-9]{9}$"; flag = text.matches(reg); return flag; } }
//package com.demo2s; import java.util.regex.Pattern; public class Main { public static void main(String[] argv) throws Exception { String str = "123456789"; System.out.println(isTeleNumber(str)); }//from w w w . j a v a 2 s . c o m private static final Pattern teleNumberPattern = Pattern.compile("^\\d{3}\\-\\d{8}|\\d{4}\\-\\d{7}$"); public static boolean isTeleNumber(String str) { return teleNumberPattern.matcher(str).matches(); } }