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:

`$format($now)`

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');
};
0
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'\)

Replace:

\$format($1)

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.

https://regex101.com/ -- a good place to test/making your regex

1

Please sign in to leave a comment.