Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...