日期:2014-05-20 浏览次数:20960 次
public class TestMoney { public static void main(String[] args) { String[] moneys = { "1,000,000.00", "1,000,000", "1000,000.00", "1000,000.00" }; String regx = "1[\\d]{0,2}(,[\\d]{3})*(\\.[\\d]{2}){0,1}"; for (String money : moneys) System.out.println(money.matches(regx)); } } //根据你给出的条件我只能,总结下面的规律 //如果有小数点,则有两位小数位 //小数点之前(或者无小数点),最高位是“1”,除了最高字段可能为1,2,3位,每个字段三位,字段之间由逗号隔开 /* true true false false */
------解决方案--------------------
public static void main(String[] args) { String[] moneys = { "0", "1", "10", "100", "1000", "1,000", "10000", "10,000", "100000", "100,000", "0.1", "0.11", "0.111", "1.1", "1.11", "1.111" }; for (String money : moneys) { Matcher m = Pattern.compile("0|[1-9]\\d{0,2}(,\\d{3})*|[1-9]\\d{0,2}(,\\d{3})*\\.\\d{1,2}|0\\.\\d{1,2}").matcher(money); System.out.println(m.matches()); } }