0

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?

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
user1090729
  • 1,523
  • 3
  • 13
  • 17
  • Which language are you trying this in? Use the right tool for the job. – Noel Apr 29 '14 at 06:31
  • This is not what regex is intended for. You would have to describe "larger as 734" as a pattern, this is complicated and error prone, see e.g. [this](http://stackoverflow.com/questions/676467/how-to-match-numbers-between-x-and-y-with-regexp?rq=1) as an example. Match all numbers and use the normal operators to check if the the number is larger, smaller, .... – stema Apr 29 '14 at 07:08

3 Answers3

2

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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

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.

NeilMacMullen
  • 3,339
  • 2
  • 20
  • 22
  • Of course you *can* do it with a regex. I'm not saying that you *should*, though. – Tim Pietzcker Apr 29 '14 at 08:17
  • I should probably have said you can't _practically_ do it with a regex. I took the OP to be asking whether there was a regex syntax to do this in a straightforward manner rather constructing massively complicated expressions. :-) – NeilMacMullen Apr 29 '14 at 08:23
-1

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

Muthu
  • 57
  • 3
  • I don't know how this answers the question, but in general, if you want to match exactly 4 digits with `[0-9]{4}` you need to use anchors, otherwise this would also match "1234" in "1234567". – stema Apr 29 '14 at 07:13