Regex for significant figures of a defined size (any language)

柔情痞子 提交于 2019-12-24 21:05:28

问题


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.

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

// 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?


回答1:


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.

See this regex in use here

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

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*$)(?:\d+[.,]\d+)) Option 2
      • (?=.{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
  • .+ Match any character one or more times
  • $ Assert position at the end of the line (not really needed but I think for readability it helps)


来源:https://stackoverflow.com/questions/46832818/regex-for-significant-figures-of-a-defined-size-any-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!