Regex replace over-selecting for no apparent reason

I guess I'm just not seeing how to format the regex to select only a portion like this:

`$now->format('Y-m-d H:i:s')`

I'm trying to convert it to something like this:


In the regex, I tried making  `(.*)` be `(.*)?` or `(.?)` (usually my fix for over-selecting), but nothing I'm trying is fixing it. I'm pretty sure my regex is bad, but I'm not seeing how to fix it.

Not that you need to know, but here's the `$format` function in case you're curious:

$format = function(\DateTime $date) {
return $date->format('Y-m-d');
1 comment

Hi there,

\$.*-> will match all from the first $ until it finds -> . This part works correctly.

Better limit it with allowed symbols only. Something like:

(\$[\w\d\_]+)->format\('Y-m-d H:i:s'\)



Test code:

$someVar = new \DateTime($now->format('Y-m-d H:i:s'));
$anotherVar = new \DateTime($tomorrow_date->format('Y-m-d H:i:s'));
$thirdVar = new \DateTime($futureDate1234->format('Y-m-d H:i:s'));

How it looks:


P.S. -- a good place to test/making your regex


Please sign in to leave a comment.