51,397
社区成员




String regex = "[1-9]\\d{3}(-(?!00)\\d{2})*";
String input = "1101-01-10";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.out.println(m.matches());
public static void main(String[] args) {
String str = "2333-11-11-55-01";
Pattern pattern = Pattern.compile("[^0]\\d{3}(-\\d{2}(?<!(00)))+");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println(matcher.group());
}
}
[1-9]\\d{3}(-\\d{2}(?!\\w{1,})(?<!-00))+
\\w比\\d好些
public static void main(String[] args) {
String str = "2333-11-11-55-01-02";
Pattern pattern = Pattern.compile("[1-9]\\d{3}(-\\d{2}(?!\\d{1,})(?<!-00))+");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println(matcher.group());
}
}
public static void main(String[] args)
{
String str1 = "1001-01-01-01-01";
String str2 = "0101-01-01-01-01";
String str3 = "1001-01";
String str4 = "1001-00-10";
String str5 = "1001-11-00";
Pattern p = Pattern.compile("([^0]\\d{3}(-(?!00)\\d{2})+)");
show(str1,p);
show(str2,p);
show(str3,p);
show(str4,p);
show(str5,p);
}
private static void show(String str,Pattern p)
{
Matcher m = p.matcher(str);
if(m.find())
{
System.out.println(m.group(1));
}
else
{
System.out.println("没匹配到");
}
}
运行结果:
1001-01-01-01-01
没匹配到
1001-01
没匹配到
1001-11