金額的正規表示式

番茄奈斯發表於2019-05-20
//金額正則(18,2)
private static final String AMT_REGEX = "^(([1-9][0-9]{0,14})|([0]{1})|(([0]\\.\\d{1,2}|[1-9][0-9]{0,14}\\.\\d{1,2})))$";
private static final String INPUT = "0.11";

public static void main(String args[]) {
    //***matches
    boolean isMatch = Pattern.matches(AMT_REGEX, INPUT);
    System.out.println("matches:"+isMatch);

    //matcher
    Pattern pattern;
    pattern = Pattern.compile(AMT_REGEX);
    Matcher matcher = pattern.matcher(INPUT);
    //不要求整個序列都匹配
    System.out.println("lookingAt(): "+matcher.lookingAt());
    //整個序列都匹配
    System.out.println("matches(): "+matcher.matches());

}

相關文章