Regexp for prohibit entering a point after entering a point and number JavaFX

假装没事ソ 提交于 2020-01-24 15:34:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!