Is there a way for a regexp to match only strings with numbers higher than a particular, multiple digit value?

StackOverflow https://stackoverflow.com/questions/23356669

  •  11-07-2023
  •  | 
  •  

Question

I know I can match numbers by doing something like [2-9] I would match only numbers 2 and higher, but what can I do for numbers with more digits?

For instance, if I wanted to match numbers higher than 734 I could do [7-9][3-9][4-9], but that would not match some numbers like 741.

Is there a proper way to do this? Also, can it be made to work for numbers with an arbitrarily large amount of digits?

Was it helpful?

Solution

You would need to spell out all the possible textual representation of allowed numbers. This is not what regexes are good at - it will work, but it's definitely hard to maintain/change:

^(73[4-9]|7[4-9]\d|[89]\d\d|\d{4,})$

matches all numbers from 734 and up.

See it live on regex101.com.

OTHER TIPS

No, you can't do this with just a regex. If you are trying to do this from within a program or script, you should use a regex to extract the digits then try to parse it as a number.

Alternatively, if you are simply just trying to process a file and don't care how it's done there are various tools which can do it for you.

Use number of digits found in your input

may be you have 'n' numbers in your input

n=4 input = 1234; or number = 8704

So, you can use

[0-9]{4}

Or Some time you need to match the Digits must be at least above 2 means

[2-9]{4}

May be i have Digits of 4 or 5

[2-9]{4,5}

Its match

8567

94877

But Not match

2

999999999

7937827857

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top