正则表达式用于定义大小的有效数字(任何语言) [英] Regex for significant figures of a defined size (any language)

查看:110
本文介绍了正则表达式用于定义大小的有效数字(任何语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本以为这很普遍,但是还没有找到解决方法

I would have thought this would be fairly common, but haven't found a solution

我想要的是一个正则表达式,它在一定数量的有效数字(最大值)上失败,但是传递的次数小于最大值.我希望它可以与点或逗号(法语)十进制分隔符一起使用.

What I would like is a regular expression that fails on a set number of significant figures (a Max), but passes for less than the Max. I would like it to work with both dot or comma (french) decimal separators.

因此对于15个有效数字,这些应该通过:
0
0.00
1
-1
1.23456789012345
10.2345678901234
12.3456789012345
-123.4
-12.34
-1,33
-1.33
-123456789012345
-1234567890123450
-12345678901234.50
12345678901234.50
123456789012345.00

So for 15 significant figures, these should pass:
0
0.00
1
-1
1.23456789012345
10.2345678901234
12.3456789012345
-123.4
-12.34
-1,33
-1.33
-123456789012345
-1234567890123450
-12345678901234.50
12345678901234.50
123456789012345.00

//应该失败:
-1234567890123456
-12345678901234.56
12345678901234.56
123456789012345.60
1.234567890123456
12.34567890123456
123456789012340.6
123456789012300.67
123456789012300000000000.67
10000000000010000000001000010000000001.22

// should fail:
-1234567890123456
-12345678901234.56
12345678901234.56
123456789012345.60
1.234567890123456
12.34567890123456
123456789012340.6
123456789012300.67
123456789012300000000000.67
10000000000010000000001000010000000001.22

我知道我需要使用负面表情,而到目前为止,我已经做到了:

I know I need to use negative look a heads, and I have got close with this so far:

^(?!(?:.*?[1-9]){15,})([-+]?\s*\d+[\.\,]?\d*?)$

https://regex101.com/r/hQ1rP0/218

但是您可以看到最后几个仍然通过了,还有指针吗?

but you can see the last few still pass, any pointers?

推荐答案

代码

对于实际的科学计数法(其中前导零在小数符号前起作用),您可以使用以下

Code

For actual scientific notation (where leading zeros matter before the decimal symbol), you can use the following

^-?(?=\d{1,15}(?:[.,]0+)?$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)))‌.+$

但是,下一个正则表达式适用于您的前导零的情况,而与小数位无关.

This next regex, however, works for your case of leading zeros regardless of decimal position.

在此处查看此正则表达式的用法

^-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)‌​)).+$


说明

  • ^在行的开头声明位置
  • -?从字面上匹配零个或-字符之一
  • (?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)))正向前瞻确保确保符合以下条件
    • \d{1,15}(?:[.,]0+)?0*$选项1
      • \d{1,15}匹配1到15之间的任何数字字符
      • (?:[.,]0+)?匹配一个十进制符号,.,然后逐字匹配一个或多个0,但零次或一次
      • 0*从字面上匹配任意数量的0 s
      • $在行尾声明位置

      • Explanation

        • ^ Assert position at the start of the line
        • -? Match zero or one of the - character literally
        • (?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+))) Positive lookahead ensuring what follows matches the following
          • \d{1,15}(?:[.,]0+)?0*$ Option 1
            • \d{1,15} Match between 1 and 15 of any digit character
            • (?:[.,]0+)? Match a decimal symbol ,., followed by one or more 0s literally, but either zero or one time
            • 0* Match any number of 0s literally
            • $ Assert position at the end of the line
              • (?=.{1,16}0*$)确保随后的内容与以下内容匹配
              • .{1,16}匹配1到16次之间的任意字符
              • 0*从字面上匹配任意数量的0 s
              • $在行尾声明位置
              • (?:\d+[.,]\d+)匹配以下内容
              • \d+匹配1到无限次之间的任意数字
              • [,.]匹配十进制字符
              • \d+匹配1到无限制时间之间的数字
              • (?=.{1,16}0*$) Ensure what follows matches the following
              • .{1,16} Match any character between 1 and 16 times
              • 0* Match any number of 0s literally
              • $ Assert position at the end of the line
              • (?:\d+[.,]\d+) Match the following
              • \d+ Match any digit between 1 and unlimited times
              • [,.] Match a decimal character
              • \d+ Match a digit between 1 and unlimited times

              这篇关于正则表达式用于定义大小的有效数字(任何语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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