Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does preg_replace with /(.*)/ repeat part of string?

Why does the following code:

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

Output abc.def.def instead of 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.

like image 434
matb33 Avatar asked May 30 '12 15:05

matb33


2 Answers

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

  1. The whole string abcabc.def
  2. The empty string → .def

which gives abc.def.def.


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

like image 57
kennytm Avatar answered Oct 10 '22 02:10

kennytm


It's the expected behaviour: 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.

like image 34
dAm2K Avatar answered Oct 10 '22 01:10

dAm2K