匹配数值类型字符串的正则表达式
public class Test {
public static void main(String[] args) {
String reg = "^-?([1-9]\\d*|0)(\\.\\d+)?$";
System.out.println("123456789".matches(reg));
System.out.println("-123456789".matches(reg));
System.out.println("123.3456789".matches(reg));
System.out.println("-123.3344556677".matches(reg));
System.out.println("0".matches(reg));
System.out.println("-0".matches(reg));//唯一遗憾是-0也能匹配上,不想改了,改的话还要一个挺长的串
System.out.println("0.0003".matches(reg));
System.out.println("0.123456".matches(reg));
System.out.println("-0.1345".matches(reg));
System.out.println("-0.0003".matches(reg));
System.out.println("--------------------");
System.out.println("2016-12".matches(reg));
System.out.println("1.3.3".matches(reg));
System.out.println("00456".matches(reg));
System.out.println("00.456".matches(reg));
System.out.println("+456".matches(reg));
System.out.println("-00.456".matches(reg));
}
}
很全的一个帖子,关于正则表达式符号的解释:
正则表达式全部符号解释
正则表达式(括号)、[中括号]、{大括号}的区别小结