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+":不正确");
}
}
}
"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}/