为什么preg_replace用/(.*)/重复部分字符串?

时间:2023-02-12 08:45:51

Why does the following code:

为什么有以下代码:

<?php echo preg_replace("/(.*)/", "$1.def", "abc");

Output abc.def.def instead of abc.def?

输出abc.def而不是abc.def?

I'm interested in understanding why the repetition occurs.

我想知道为什么重复发生。

Using /(.+)/ or /^(.*)$/ works as expected, but I'm not looking for a solution, just asking a question (although these patterns may be related to the answer).

使用/(+)/或/ ^(. *)/美元像预期的那样工作,但我不寻求一个解决方案,只是在问一个问题(尽管这些模式可能与答案)。

Tinker with a live version here.

这里有一个实时版本的Tinker。

3 个解决方案

#1


8  

Because .* matches the empty substring at the end of the string. It means there are two matches to the string abc:

因为.*匹配字符串末尾的空子字符串。它表示有两个匹配字符串abc:

  1. The whole string abcabc.def
  2. 整个字符串abc→abc.def
  3. The empty string → .def
  4. →. def的空字符串

which gives abc.def.def.

它给abc.def.def。


Edit: Detail of why it happens is explained in String.replaceAll() anomaly with greedy quantifiers in regex.

编辑:在regex中使用贪婪量词的String.replaceAll()异常解释了为什么会发生这种情况。

#2


3  

It's the expected behaviour: https://bugs.php.net/bug.php?id=53855

这是预期的行为:https://bugs.php.net/bug.php?id=53855

This is expected behaviour and nothing peculiar to PHP. The * quantifier allows an "empty" match to occur at the end of your subject string.

这是预期的行为,不是PHP特有的。量词允许在主题字符串的末尾出现“空”匹配。

#3


2  

If you make your regex non-greedy, /(.*?)/ you can see the whole process of repetition working on a much larger/noticeable scale:

如果你使你的regex不贪婪,/(.*?)/你可以看到重复工作的整个过程在一个更大/显著的范围内:

.defa.defb.defc.def

You get four matches: a, b, c, empty. Whereas, as other people mentioned, with a greedy regex, you get 2 matches, the full string, and an empty string.

你有四根火柴:a, b, c,空着。然而,正如其他人提到的,使用贪婪的regex,您将得到两个匹配项、完整的字符串和一个空字符串。

#1


8  

Because .* matches the empty substring at the end of the string. It means there are two matches to the string abc:

因为.*匹配字符串末尾的空子字符串。它表示有两个匹配字符串abc:

  1. The whole string abcabc.def
  2. 整个字符串abc→abc.def
  3. The empty string → .def
  4. →. def的空字符串

which gives abc.def.def.

它给abc.def.def。


Edit: Detail of why it happens is explained in String.replaceAll() anomaly with greedy quantifiers in regex.

编辑:在regex中使用贪婪量词的String.replaceAll()异常解释了为什么会发生这种情况。

#2


3  

It's the expected behaviour: https://bugs.php.net/bug.php?id=53855

这是预期的行为:https://bugs.php.net/bug.php?id=53855

This is expected behaviour and nothing peculiar to PHP. The * quantifier allows an "empty" match to occur at the end of your subject string.

这是预期的行为,不是PHP特有的。量词允许在主题字符串的末尾出现“空”匹配。

#3


2  

If you make your regex non-greedy, /(.*?)/ you can see the whole process of repetition working on a much larger/noticeable scale:

如果你使你的regex不贪婪,/(.*?)/你可以看到重复工作的整个过程在一个更大/显著的范围内:

.defa.defb.defc.def

You get four matches: a, b, c, empty. Whereas, as other people mentioned, with a greedy regex, you get 2 matches, the full string, and an empty string.

你有四根火柴:a, b, c,空着。然而,正如其他人提到的,使用贪婪的regex,您将得到两个匹配项、完整的字符串和一个空字符串。