I've been using the following site to test a PHP regex so I don't have to constantly upload: http://www.spaweditor.com/scripts/regex/index.php
I'm using the following regex:
/(.*?)\.{3}/
on the following string (replacing with nothing):
Non-important data...important data...more important data
and preg_replace is returning:
more important data
yet I expect it to return:
important data...more important data
I thought the ? is the non-greedy modifier. What's going on here?
Your non-greedy modifier is working as expected. But
preg_match
replaces all occurences of the the (non-greedy) match with the replacement text (""
in your case). If you want only the first one replaced, you could pass1
as the optional 4th argument (limit) topreg_replace
function (PHP docs for preg_replace). On the website you linked, this can be accomplished by typing1
into the text input between the word "Flags" and the word "limit".just an actual example of @Asaph solution. In this example ou don't need non-greediness because you can specify a count. replace just the first occurrence of @ in a line with a marker