package leetcode;

/**
 * https://leetcode.com/problems/valid-number/
 */
public class Problem65 {
    public boolean isNumber(String s) {
        return s.matches("\\s*(?:-|\\+)?(\\d+|(?:\\d+\\.\\d*)|(?:\\d*\\.\\d+))(e(?:-|\\+)?\\d+)?\\s*");
    }

    private static void test(boolean actual, boolean expected) {
        if (actual != expected) {
            throw new RuntimeException("Wrong!");
        }
    }

    public static void main(String[] args) {
        Problem65 prob = new Problem65();
        test(prob.isNumber("0"), true); // true
        test(prob.isNumber("123.45"), true); // true
        test(prob.isNumber(" 0.1"), true); // true
        test(prob.isNumber("abc"), false); // false
        test(prob.isNumber("1 a"), false); // false
        test(prob.isNumber("2e10"), true); // true
        test(prob.isNumber("1.2e10"), true); // true
        test(prob.isNumber("1.2e-10"), true); // true
        test(prob.isNumber(".1"), true); // true
        test(prob.isNumber(""), false); // false
        test(prob.isNumber("e9"), false); // false
        test(prob.isNumber("1.21.2"), false); // false
        test(prob.isNumber("1.2e1.2"), false); // false
        test(prob.isNumber("3."), true); // true
        test(prob.isNumber("."), false); // false
        test(prob.isNumber("-12."), true); // true
        test(prob.isNumber("-.234"), true); // true
        test(prob.isNumber("-2.2e3.4"), false); // false
        test(prob.isNumber("-2.2e-3.4"), false); // false
        test(prob.isNumber("+.8"), true); // true
        test(prob.isNumber(" 005047e+6"), true); // true
    }
}

@@ -245,1365 +245,7 @@
    %7D
-%0A%0A    private static void test(boolean actual, boolean expected) %7B%0A        if (actual != expected) %7B%0A            throw new RuntimeException(%22Wrong!%22);%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem65 prob = new Problem65();%0A        test(prob.isNumber(%220%22), true); // true%0A        test(prob.isNumber(%22123.45%22), true); // true%0A        test(prob.isNumber(%22 0.1%22), true); // true%0A        test(prob.isNumber(%22abc%22), false); // false%0A        test(prob.isNumber(%221 a%22), false); // false%0A        test(prob.isNumber(%222e10%22), true); // true%0A        test(prob.isNumber(%221.2e10%22), true); // true%0A        test(prob.isNumber(%221.2e-10%22), true); // true%0A        test(prob.isNumber(%22.1%22), true); // true%0A        test(prob.isNumber(%22%22), false); // false%0A        test(prob.isNumber(%22e9%22), false); // false%0A        test(prob.isNumber(%221.21.2%22), false); // false%0A        test(prob.isNumber(%221.2e1.2%22), false); // false%0A        test(prob.isNumber(%223.%22), true); // true%0A        test(prob.isNumber(%22.%22), false); // false%0A        test(prob.isNumber(%22-12.%22), true); // true%0A        test(prob.isNumber(%22-.234%22), true); // true%0A        test(prob.isNumber(%22-2.2e3.4%22), false); // false%0A        test(prob.isNumber(%22-2.2e-3.4%22), false); // false%0A        test(prob.isNumber(%22+.8%22), true); // true%0A        test(prob.isNumber(%22 005047e+6%22), true); // true%0A    %7D
 %0A%7D%0A

Solve problem 65 (regex one-liner FTW!)