Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for multiple of 1000 accepting values greater than 25000 and decimal not allowed

Tags:

regex

I have two combinations:

  1. For checking multiple of 1000 which is ^[1-9]+[0-9]*000$.

  2. For checking value greater than 25000 which is ^\\d{6,}$|^[3-9]\\d{4}$|^2[5-9]\\d{3}$.

When I tried to combine two regex with positive lookahead operator like below,

^(?=[1-9]+[0-9]*000)(?=\\d{6,}$|^[3-9]\\d{4}$|^2[5-9]\\d{3})*$

Its taking values such as 25000100,35000100,which is not a multiple of 1000.

Is it possible to achieve both scenarios through one regex?

like image 983
Gaurav Bhardwaj Avatar asked Jan 22 '16 17:01

Gaurav Bhardwaj


3 Answers

This should work

^([1-9]\d{2,}|[3-9]\d|2[5-9])000$

https://regex101.com/r/vJ8xU6/3

like image 81
Brendan Abel Avatar answered Oct 04 '22 12:10

Brendan Abel


^(2[5-9]\d*|[3-9]\d+|[12]\d{2,})000$

See it in action

The idea is:

  • 2[5-9]\d* - 25-29 with optional digits after that is >= 25
  • [3-9]\d+ - 3-9 with one or more digits after that is > 25
  • [12]\d{2,} - 1 or 2 with two or more digits after that is > 25
  • 000 - add three zeroes at the end for 1000 divisibility
like image 31
ndnenkov Avatar answered Oct 04 '22 13:10

ndnenkov


Obviously any string ending in 000 is a multiple of 1000, so given a string X000, let's just worry about the X.

  • If X is >=3 digits (matches [1-9]\d\d\d*), then X000 >= 100000 > 25000
  • If X is 2 digits and the first digit of X is > 2 (matches [3-9]\d), then X000 >= 30000 > 25000
  • If X is 2 digits and the first digit of X is 2 and the second digit of X is >= 5 (matches 2[5-9]) then X000 >= 25000 = 25000.

Any other possible X is going to result in a smaller number (or something that isn't a number), so just throw those together into a group ([1-9]\d\d\d*|[3-9]\d|2[5-9]) for the prefix.

([1-9]\d\d\d*|[3-9]\d|2[5-9])000

See Also: https://regex101.com/r/bV1kN3/1

like image 26
QuestionC Avatar answered Oct 04 '22 12:10

QuestionC