【问题标题】:regex for numerics and decimals in javajava中数字和小数的正则表达式
【发布时间】:2014-09-18 10:26:09
【问题描述】:

需要一个允许以下有效值的正则表达式。(只允许小数和数字)

有效:

.1  
1.10  
1231313  
0.32131  
31313113.123123123 

无效:

dadadd.31232  
12313jn123  
dshiodah  

【问题讨论】:

    标签: java regex validation


    【解决方案1】:

    试试这个:

    String input = "0.32131";
    
    Pattern pat = Pattern.compile("\\d*\\.?\\d+");
    Matcher mat = pat.matcher(input);
    
    if (mat.matches())
        System.out.println("Valid!");
    else
        System.out.println("Invalid");
    

    【讨论】:

    • 完美,谢谢。只需要另一个正则表达式,仅用于小数而不是数字有效 :: .1,0.113131,4242424.23423424
    • 我也想知道pattern.compile的需要是什么,我们可以像string.matches(regex)那样直接做,它会返回true或false
    • 你的表达式会说0.321.31是有效的
    • 哎呀@alfasin 感谢指出,我没有检查这个值
    • @user2841408 我的表情现在已经修复了。关于您的问题:当需要重用正则表达式时,使用 Pattern.compile() 是一个好主意。这样,我们只需要创建 一次,但可以根据需要多次匹配结果。这比使用String.matches() 更有效,后者每次调用时都会重新编译相同的表达式。
    【解决方案2】:

    你可以试试正则表达式:

    ^(\d+|\d*\.\d+)$
    

    * 使用Debuggex: Online visual regex tester 生成的图像。

    这个正则表达式的解释:

    NODE                     EXPLANATION
    --------------------------------------------------------------------------------
      ^                        the beginning of the string
    --------------------------------------------------------------------------------
      (                        group and capture to \1:
    --------------------------------------------------------------------------------
        \d+                      digits (0-9) (1 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
       |                        OR
    --------------------------------------------------------------------------------
        \d*                      digits (0-9) (0 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
        \.                       '.'
    --------------------------------------------------------------------------------
        \d+                      digits (0-9) (1 or more times (matching
                                 the most amount possible))
    --------------------------------------------------------------------------------
      )                        end of \1
    --------------------------------------------------------------------------------
      $                        before an optional \n, and the end of the
                               string
    

    * 来自Explain Regular Expressions的解释。

    【讨论】:

      【解决方案3】:

      如果您想严格限制允许的匹配:

      ^[0-9]*\.?[0-9]+$
      

      解释

      ^         # the beginning of the string
       [0-9]*   #  any character of: '0' to '9' (0 or more times)
       \.?      #  '.' (optional)
       [0-9]+   #  any character of: '0' to '9' (1 or more times)
      $         # before an optional \n, and the end of the string
      

      Live Demo

      【讨论】:

        猜你喜欢
        • 2021-08-04
        • 2012-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多