I'm trying to make a regex, that checks if something is a price. I'm not good at regular expressions and this one give me a regex syntax error. It should check if the input is something like 13.3 or 0.1 or .4 or 6 or 200 or 3.04, which could all be interpreted as a price. I tried using Double.valueOf(String), however, this method accepts strings like 3.00, so more than 2 floating points.
private boolean validateAndSet(JTextField tf) {
boolean isDouble = true;
try {
if (tf.getText().matches("^[0-1]*(\\.)?[0-1]{0-2}$")) {
item.setAlertPrice(Double.valueOf(tf.getText()));
return true;
}
isDouble = false;
} catch (NumberFormatException nfe) {
isDouble = false;
} finally {
if (!isDouble) {
System.out.println("Not a price;");
tf.setText("0.00");
}
}
return isDouble;
}