Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for 6 digits before and 2 digits after decimal

I need a regex in which decimal is optional. If decimal is there then there can be max 6 digits before and max 2 digits after decimal. If decimal is not there then max of 6 digits is valid.

Regex tested :^\d{0,6}\.?\d{1,2}$

The above regex allows max of 8 digits without decimal. How can I change according to my needs so that if there is no decimal then it would take max of 6 digits?

VALID CASES

123456.12  
21231  
123456  
15465.43  
23.34  
6.45  
.12

INVALID CASES

12345678  
123456.331  
like image 893
Harish Gupta Avatar asked Feb 19 '15 17:02

Harish Gupta


1 Answers

^\d{0,6}(\.\d{1,2})?$

Try this.See demo.

https://regex101.com/r/oL9kE8/4

Just make the (\.\d{1,2}) decimal part optional.?

like image 124
vks Avatar answered Oct 20 '22 20:10

vks