Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Regex只允许数字和点_Java_Regex_Validation - Fatal编程技术网

Java中的Regex只允许数字和点

Java中的Regex只允许数字和点,java,regex,validation,Java,Regex,Validation,我正在努力克服文本字段的限制,在这里只能输入除以点的数字。以下是我得到的: public static void allowOnlyNumbersAndPoints(final TextField tf) { tf.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(final Observabl

我正在努力克服文本字段的限制,在这里只能输入除以点的数字。以下是我得到的:

 public static void allowOnlyNumbersAndPoints(final TextField tf) {
        tf.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(final ObservableValue<? extends String> ov, final String oldValue, final String newValue) {
                if (!newValue.matches("\\d+(\\.\\d+)*$")) {
                    tf.setText(newValue.replaceAll("[^\\d]", ""));
                }
            }
        });

    }
public static void allownlynumbersandpoints(最终文本字段tf){
tf.textProperty().addListener(新的ChangeListener()){
@凌驾

public void已更改(最终可观察值您必须在调用
replaceAll
时使用regex
“[^0-9]”
,以替换任何不是数字的内容。
“[^\\d]”“
(您当前拥有的)不应该工作,因为命名组在方括号内不起作用。

您必须使用regex
“[^0-9]”
在调用
replaceAll
以替换任何非数字。
“[^\\d]”“
(您当前拥有的)不应该工作,因为命名组在方括号内不工作。

这可能工作
\d+[\d.]*[\d$]*


您是指多个点还是仅一个点?

这可能有效
\d+[\d.]*[\d$]*


您是指多个点还是仅一个点?

您可以使用
\\d+\\.?\\d*

演示:

public class Main {
    public static void main(String[] args) {
        String[] testStrs = { "100", "123.23", "a1", "1a", "a1.2", "0.23" };
        for (String s : testStrs) {
            System.out.println(s + (s.matches("\\d+\\.?\\d*") ? " matches " : " doesn't match"));
        }
    }
}
100 matches 
123.23 matches 
a1 doesn't match
1a doesn't match
a1.2 doesn't match
0.23 matches 
输出:

public class Main {
    public static void main(String[] args) {
        String[] testStrs = { "100", "123.23", "a1", "1a", "a1.2", "0.23" };
        for (String s : testStrs) {
            System.out.println(s + (s.matches("\\d+\\.?\\d*") ? " matches " : " doesn't match"));
        }
    }
}
100 matches 
123.23 matches 
a1 doesn't match
1a doesn't match
a1.2 doesn't match
0.23 matches 

您可以使用
\\d+\.?\\d*

演示:

public class Main {
    public static void main(String[] args) {
        String[] testStrs = { "100", "123.23", "a1", "1a", "a1.2", "0.23" };
        for (String s : testStrs) {
            System.out.println(s + (s.matches("\\d+\\.?\\d*") ? " matches " : " doesn't match"));
        }
    }
}
100 matches 
123.23 matches 
a1 doesn't match
1a doesn't match
a1.2 doesn't match
0.23 matches 
输出:

public class Main {
    public static void main(String[] args) {
        String[] testStrs = { "100", "123.23", "a1", "1a", "a1.2", "0.23" };
        for (String s : testStrs) {
            System.out.println(s + (s.matches("\\d+\\.?\\d*") ? " matches " : " doesn't match"));
        }
    }
}
100 matches 
123.23 matches 
a1 doesn't match
1a doesn't match
a1.2 doesn't match
0.23 matches 
像这样试试

String r = "(?:\\d+)?[\\.]?\\d+|\\d+\\.";

for (String num : new String[] { "2.33", "33.3", "33", ".33",
        "44.", "4.4.4", "a.3", "44.k" }) {
    System.out.printf("%5s : %s%n", num,
            " " + (num.matches(r) ? "valid" : "not valid"));

}
印刷品

 2.33 :  valid
 33.3 :  valid
   33 :  valid
  .33 :  valid
  44. :  valid
4.4.4 :  not valid
  a.3 :  not valid
 44.k :  not valid
像这样试试

String r = "(?:\\d+)?[\\.]?\\d+|\\d+\\.";

for (String num : new String[] { "2.33", "33.3", "33", ".33",
        "44.", "4.4.4", "a.3", "44.k" }) {
    System.out.printf("%5s : %s%n", num,
            " " + (num.matches(r) ? "valid" : "not valid"));

}
印刷品

 2.33 :  valid
 33.3 :  valid
   33 :  valid
  .33 :  valid
  44. :  valid
4.4.4 :  not valid
  a.3 :  not valid
 44.k :  not valid
通常最好改为,这样就不会限制用户的编辑能力。另外,您不必假设
是十进制分隔符(因为在许多地区,它不是)。例如,
tf.setTextFormatter(new TextFormatter(new NumberStringConverter()))
通常最好改为,这样您就不会禁止用户进行编辑。另外,您不必假设
是十进制分隔符(因为在许多地区,它不是)。例如,
tf.setTextFormatter(new TextFormatter(new NumberStringConverter());