问题
I need input consecutive integers and real numbers separated by commas, like this: 2,12.4,3
I forbid entering sequentially two commas, two points, and all other characters, except numbers, by the following expression: (?!.*[\\.,]{2,})[\\d,\\.]*
But with it I can type this: 2,12.4.3
That is, after entering a point and a number, it is possible to enter once again a point, and there must be only a number or comma.
I need to leave all the conditions from the previous expression, and deny entering the point, if before this point, through the numbers (\d+), there is a point.
@FXML
private TextField tf;
@Override
public void initialize(URL url, ResourceBundle rb) {
tf.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
if (!newValue.matches("(?!.*[\\.,]{2,})[\\d,\\.]*")) {
((StringProperty) observable).setValue(oldValue);
}
});
}
回答1:
You should be more specific in finding a solution:
^(?:\\d+(?:\\.\\d+)?,)*\\d+(?:\\.\\d+)?$
Live demo
来源:https://stackoverflow.com/questions/44746807/regexp-for-prohibit-entering-a-point-after-entering-a-point-and-number-javafx