3
preg_match_all('/(\d{4})/', $text, $matches);

Using above function we can extract exact 4 digit numbers from a given string.

How can extract the numbers that contains greater than 4 digits using regular expression..

Is there any way to specify the minimum length in regular expressions ?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Muthu Krishnan
  • 1,664
  • 2
  • 10
  • 15

4 Answers4

6

Yes, you can specify the minimum length:

/(\d{4,})/

The brace syntax accepts a single number (as you used) indicating the exact number of repetitions to match, but it also allows specifying the minimum and maximum number of repetitions to match, separated by a comma. If the maximum is omitted (but the comma isn't), as in this answer, then the maximum is unbounded. The minimum can also be omitted, which is the same as explicitly specifying a minimum of 0.

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • yes working fine.Also preg_match_all('/(\d{4,})/', $text, $matches); $matches returns 2 arrays contains same result.Please guide me. – Muthu Krishnan Dec 29 '11 at 06:57
  • @Muthu: Sorry, I don't know PHP (just regexes). Ask another question ;-) – Cameron Dec 29 '11 at 07:00
  • 1
    The first element in the array is the full match, the second is the match in the parenthesis (first capture). Remove the parenthesis for a single element in the array, or use either index. – Aram Kocharyan Dec 29 '11 at 07:01
1

You'll want something like this:

\d{4,}

So:

preg_match_all('#(\d{4,})#', $text, $matches);

Note that I used # as the delimiter here, I find it easier to look at :)

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
1

\d{4,}

Should do it. This sets 4 to minimum.

Stan Wiechers
  • 1,962
  • 27
  • 45
1
preg_match_all('/(\d{4,})/', $text, $matches);
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
Oleg
  • 2,733
  • 7
  • 39
  • 62