简体   繁体   中英

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  
^\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. ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM