PHP Regex(+和*之间的不同结果)

时间:2022-06-09 12:10:00

If I put * in my regex, it doesn't work, if I put +, it works.

如果我在regex中输入*,它就不会工作,如果我输入+,它就会工作。

* should mean 0 or more, + should mean 1 or more.

*表示0或更多,+表示1或更多。


Case with *

情况*

$num = '    527545855   ';
var_dump( preg_match( '/\d*/', substr( $num, 0, 18 ), $coincidencias ) );
var_dump($coincidencias);exit;

Result:

结果:

int(1)
array(1) {
  [0]=>
  string(0) ""
}

Case with +

情况+

$num = '    527545855   ';
var_dump( preg_match( '/\d+/', substr( $num, 0, 18 ), $coincidencias ) );
var_dump($coincidencias);exit;

Result:

结果:

int(1)
array(1) {
  [0]=>
  string(9) "527545855"
}

I thought both should work, but I don't understand why the * doesn't work.

我认为两者都应该奏效,但我不明白为什么*不起作用。

5 个解决方案

#1


3  

It would match the digits or nothing

它会匹配数字或什么都不匹配

\d* means match 0 to many digits

\d*表示匹配0到多个数字


For Example in

例如在

hello 123

你好,123

the regex \d*

正则表达式\ d *

would match 8 times i.e 1 at start(^) ,6 times for hello and 1 time for 123

匹配8乘以i。e 1(^)开始,6 * 123年你好和1次

#2


7  

* means 0 or more occurences thus the first occurence is the void string in your test string

*表示出现0或更多,因此第一次出现的是测试字符串中的void字符串

#3


4  

The engine will attempting to match starting from index 0.

引擎将尝试从索引0开始匹配。

  • Since \d* can match an empty string, the engine will return the empty string at index 0.
  • 因为\d*可以匹配空字符串,所以引擎将返回索引0中的空字符串。
  • In contrast, \d+ must match at least one digit, and since the quantifier is greedy, it will return the nearest sequence of digits, while taking as many digits as possible (in your case, it is the whole sequence of digits in the input string).
  • 相比之下,\d+必须匹配至少一个数字,由于量词是贪婪的,它将返回最近的数字序列,同时取尽可能多的数字(在您的例子中,它是输入字符串中的整个数字序列)。

#4


4  

You answered this in your question:

你的回答是:

* should mean 0 or more, + should mean 1 or more.

*表示0或更多,+表示1或更多。

The first thing that it matched was 0 or more digits.

它首先匹配的是0或更多的数字。

#5


2  

The * in a regex takes the character before it and looks for 0 or more instances. The + in a regex looks for 1 or more instances.

regex中的*取其前面的字符,并查找0或更多的实例。regex中的+查找一个或多个实例。

Since you aren't really checking around the number (caring for other items) you will get more items back from the regex with * since it allows for the 0 instance clause to be met, which is valid for all your spaces before the number (a space is matching 0 instances of a number). The + ensures at least one is found.

因为你并不是真的检查(照顾其他物品)数量你将获得更多的物品从正则表达式*因为它允许0实例的条款,这是有效的之前,你所有的空间数量(空间匹配0数字)的实例。+确保至少找到一个。

#1


3  

It would match the digits or nothing

它会匹配数字或什么都不匹配

\d* means match 0 to many digits

\d*表示匹配0到多个数字


For Example in

例如在

hello 123

你好,123

the regex \d*

正则表达式\ d *

would match 8 times i.e 1 at start(^) ,6 times for hello and 1 time for 123

匹配8乘以i。e 1(^)开始,6 * 123年你好和1次

#2


7  

* means 0 or more occurences thus the first occurence is the void string in your test string

*表示出现0或更多,因此第一次出现的是测试字符串中的void字符串

#3


4  

The engine will attempting to match starting from index 0.

引擎将尝试从索引0开始匹配。

  • Since \d* can match an empty string, the engine will return the empty string at index 0.
  • 因为\d*可以匹配空字符串,所以引擎将返回索引0中的空字符串。
  • In contrast, \d+ must match at least one digit, and since the quantifier is greedy, it will return the nearest sequence of digits, while taking as many digits as possible (in your case, it is the whole sequence of digits in the input string).
  • 相比之下,\d+必须匹配至少一个数字,由于量词是贪婪的,它将返回最近的数字序列,同时取尽可能多的数字(在您的例子中,它是输入字符串中的整个数字序列)。

#4


4  

You answered this in your question:

你的回答是:

* should mean 0 or more, + should mean 1 or more.

*表示0或更多,+表示1或更多。

The first thing that it matched was 0 or more digits.

它首先匹配的是0或更多的数字。

#5


2  

The * in a regex takes the character before it and looks for 0 or more instances. The + in a regex looks for 1 or more instances.

regex中的*取其前面的字符,并查找0或更多的实例。regex中的+查找一个或多个实例。

Since you aren't really checking around the number (caring for other items) you will get more items back from the regex with * since it allows for the 0 instance clause to be met, which is valid for all your spaces before the number (a space is matching 0 instances of a number). The + ensures at least one is found.

因为你并不是真的检查(照顾其他物品)数量你将获得更多的物品从正则表达式*因为它允许0实例的条款,这是有效的之前,你所有的空间数量(空间匹配0数字)的实例。+确保至少找到一个。