正则表达式,十进制前6位和小数后2位 [英] regex for 6 digits before and 2 digits after decimal

查看:125
本文介绍了正则表达式,十进制前6位和小数后2位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式,其中十进制是可选的.如果存在小数,则小数前最多6位,小数后最多2位.如果没有小数,则最多6位数字有效.

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.

已测试正则表达式: ^ \ d {0,6} \.?\ d {1,2} $

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

以上正则表达式最多允许8位数字,不带小数.如何根据需要进行更改,以便如果没有小数,那么最多需要6位数字?

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?

有效案例

123456.12  
21231  
123456  
15465.43  
23.34  
6.45  
.12

无效案例

12345678  
123456.331  

推荐答案

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

尝试一下.请参见演示.

Try this.See demo.

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

只需将(\.\ d {1,2})小数部分设置为可选.?

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

这篇关于正则表达式,十进制前6位和小数后2位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆