简体   繁体   English

正则表达式仅验证 0-9 的数字,最多 8 位数字直到一个点,最多只有两位小数

[英]Regex to validate only digits from 0-9, maximum of 8 digits till a dot and only two decimals as maximum

I haven't used regular expressions at all, so I'm having difficulty troubleshooting.我根本没有使用过正则表达式,所以我很难排除故障。 I'm looking for a regex that can help me to validate if there are only digits from 0-9, only 8 digits till a dot and two decimals as maximum.我正在寻找一个正则表达式,它可以帮助我验证是否只有 0-9 的数字,只有 8 个数字直到一个点,最多有两个小数。

For example:例如:

33445566.09 33445566.09

/[0-9]/
/[0-9]{1,8}\.[0-9]{1,2}/

If you want to just match or not如果您只想匹配或不匹配

[0-9] means match 0 or 1 or... 9 [0-9]表示匹配 0 或 1 或... 9

{1,8} means it can match minimum 1 and maximum 8 {1,8}表示它可以匹配最小 1 和最大 8

\. means escape.意味着逃跑。

If you want to get the group just use () in the regex如果您想获得该组,只需在正则表达式中使用 ()

/([0-9]{1,8})\.([0-9]{1,2})/

This way you can get 33445566 and 09这样就可以得到3344556609

Try this:尝试这个:

    String input = "123456789.12";
    Pattern pattern = Pattern.compile("^[0-9]{1,8}\\.[0-9]{1,2}$");
    Matcher matcher = pattern.matcher(input);
    System.out.println(matcher.matches()); //false
  • ^ start of line ^ 行首
  • $ end of line $ 行尾
  • [0-9] only 0123456789 [0-9] 仅0123456789
  • {1,8} symbols count from 1 to 8 include {1,8} 符号计数从 1 到 8 包括

I think you need我想你需要

\d{1,8}(.\d{1,2})? \d{1,8}(.\d{1,2})?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使输入仅接受两个小数并最多包含10个数字? - How to make an input to accept only two decimals and have maximum of 10 digits? 8 位数字的正则表达式,然后是 1 个点,之后只有 2 位数字 - Regex for 8 digits and after that 1 dot and after that only 2 digits 正则表达式验证 3 位数字和带小数点的两位小数 - Regex validate for 3 digits and two decimals with a decimal point \ d只匹配0-9位数? - \d only matchs 0-9 digits? JavaScript 中的正则表达式允许最多 10 位数字,数字之间只有一个下划线,并防止在前 5 位数字后输入下划线 - Regex in JavaScript which allow maximum 10 digits, only one underscore in between the digits and prevent entering underscore after the first 5 digits 限制“文本字段”以magento形式仅接受数字和最多10个数字 - restrict the “textfield” to accept only digits and maximum 10 digits in magento form 最大数为 100 的正则表达式,删除第二个点并将最大位数限制为 6 - Regex for max number 100, removing the second dot and restrict maximum digits to 6 Joi 验证可以包含数字 0-9 但不能仅包含数字的字符串? - Joi validate a string that can contain digits 0-9 but cannot contain only digits? 正则表达式使用JavaScript在JSP中仅允许使用0-9位数(甚至不包括'。'[Dot]) - Regular Expression to allow only 0-9 digits only (Not even '.' [Dot]) in JSP using javascript AngularJS输入ng-model仅包含两个小数或数字 - Angularjs input ng-model only with two decimals or digits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM