简体   繁体   English

从字符串php正则表达式中提取大于4位数字

[英]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.使用上面的函数,我们可以从给定的字符串中提取精确的 4 位数字。

How can extract the numbers that contains greater than 4 digits using regular expression..如何使用正则表达式提取包含大于 4 位数字的数字..

Is there any way to specify the minimum length in regular expressions ?有没有办法在正则表达式中指定最小长度?

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.最小值也可以省略,这与明确指定最小值为 0 相同。

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 :)请注意,我在这里使用#作为分隔符,我发现它更易于查看:)

\\d{4,} \\d{4,}

Should do it.应该做。 This sets 4 to minimum.这将 4 设置为最小值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM