Home > Enterprise >  Working with RegEx, but I dont know how to approach my issue
Working with RegEx, but I dont know how to approach my issue

Time:09-14

^\d{1,12}$|(?=^.{1,15}$)^\d \.\d{1,2}$

This is the expression I currently have.

I want the max limit to be 100 000 000 000 with optional two decimals max, but if the user only adds 1 decimal, they can bump the value to 100 000 000 0001.1 if they want to.

How can I approach this issue? and is there any way to make 100 000 000 000 the max value? (Not 999 999 999 999)

CodePudding user response:

^\d{1,12}$|(?=^.{1,15}$)^\d{1,12}\.\d{1,2}$

It seems the problem is the plus sign in the second part of the regex

CodePudding user response:

For the part without the decimal I tried this :

^\d{1,11}$|^100000000000$|(?=^.{1,15}$)^\d .\d{1,2}$

// works : 100000000000

// doesn't work : 999999999999

I don't know for the decimal part.

CodePudding user response:

You can use the following regex to match numbers from 0 to 99999999999 with any two digits in the decimal part or 100000000000 with only zeros in the decimal part:

^(?:(?:[0-9]|[1-9][0-9]{1,11})(?:\.\d{1,2})?|10{11}(?:\.00?)?)$

See the regex demo.

  • ^ - start of string
  • (?: - start of a non-capturing group:
    • (?:[0-9]|[1-9][0-9]{1,11}) - 0 to 99999999999 number
    • (?:\.\d{1,2})? - an optional sequence of a . char and then one or two digits
    • | - or
    • 10{11}(?:\.00?)? - 1 and then 11 zeros optionally followed with . and one or two zeros
  • ) - end of the group
  • $ - end of string.

CodePudding user response:

try this

^\d{1,12}(?:\.\d{1,2})?$

https://regex101.com/r/FcRDII/1

for the max value being 100 000 000 000, i would not validate that with regex, but if you have no other options this might work

^(?:1[0]{11}|\d{1,11})(?:\.\d{1,2})?$

https://regex101.com/r/V6b1Fv/1

  • Related