http://topic.csdn.net/u/20091010/17/4344062a-569a-4ece-bf3f-a8968996035e.html

解决方案 »

  1.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class RegexTest {
    public static void main(String[] args) {
    String[] strArray = {"123-111111","0532-8888888", "0532-88888888","001-8888888","001-88888888"
    ,"8888888","88888888","123123"};
    Pattern pattern = Pattern.compile("([0-9]{3,4}-)?[0-9]{7,8}");
    Matcher m = null;
    for (String str : strArray) {
    m=pattern.matcher(str);
    if (m.find()) 
    System.out.println(str+":正确");
    else
    System.out.println(str+":不正确");
    }
    }
    }
      

  2.   

      public static void main(String[] args) {        String[] strArray = { "123-123456", "0532-1234567", "0532-12345678",
                    "001-1234567", "001-12345678", "1234567", "12345678", "123456" };
            
            String regex = "(\\d{3,4}[-]?)?\\d{7,8}";
            
            for (String s : strArray) {
                System.out.println(s + " " + s.matches(regex));
            }
        }var regex = /(\d{3,4}[-]?)?\d{7,8}/