Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract greater than 4 digit numbers from a string php regular expression

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 ?

like image 313
Muthu Krishnan Avatar asked Feb 01 '26 22:02

Muthu Krishnan


2 Answers

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.

like image 123
Cameron Avatar answered Feb 04 '26 13:02

Cameron


\d{4,}

Should do it. This sets 4 to minimum.

like image 39
Stan Wiechers Avatar answered Feb 04 '26 12:02

Stan Wiechers