java中数字和小数的正则表达式

java中数字和小数的正则表达式,java,regex,validation,Java,Regex,Validation,需要允许以下有效值的正则表达式。(只允许小数和数字) 有效: .1 1.10 1231313 0.32131 31313113.123123123 dadadd.31232 12313jn123 dshiodah 无效: .1 1.10 1231313 0.32131 31313113.123123123 dadadd.31232 12313jn123 dshiodah 试试这个: String input = "0.32131

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

有效:

.1  
1.10  
1231313  
0.32131  
31313113.123123123 
dadadd.31232  
12313jn123  
dshiodah  
无效:

.1  
1.10  
1231313  
0.32131  
31313113.123123123 
dadadd.31232  
12313jn123  
dshiodah  
试试这个:

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");

如果要严格控制允许的匹配项:

^[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

您可以尝试使用正则表达式:

^(\d+|\d*\.\d+)$
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

*使用生成的图像

此正则表达式的解释如下:

^(\d+|\d*\.\d+)$
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

*来自的解释。

非常好,谢谢。只需要另一个正则表达式,仅用于小数,而不是有效的数字::.1,0.1131314242424.23423424另外,我想知道pattern.compile的需要是什么。我们可以像string.matches(正则表达式)一样直接执行这将返回真或假你的表达式将说,
0.321.31
是validoops@alfasin感谢指出,我没有检查这个value@user2841408我的表情现在固定了。关于您的问题:当需要重用正则表达式时,使用
Pattern.compile()
是一个好主意。通过这种方式,我们只需要创建一次,但匹配结果的次数可以根据需要而定。这比使用
String.matches()
更有效,后者每次调用时都会重新编译相同的表达式。