Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for numerics and decimals in java

Need a regex that allows the following valid values.(only decimals and numbers are allowed)

valid :

.1  
1.10  
1231313  
0.32131  
31313113.123123123 

Invalid :

dadadd.31232  
12313jn123  
dshiodah  
like image 579
Harish Gupta Avatar asked Sep 05 '25 03:09

Harish Gupta


1 Answers

If you want to be strict on your allowed matches:

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

Explanation:

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

like image 97
hwnd Avatar answered Sep 07 '25 19:09

hwnd