What is the difference between using a greedy and non-greedy approach in regular expressions in PHP?

When using regular expressions in PHP, the difference between a greedy and non-greedy approach lies in how the regex engine matches patterns. Greedy matching tries to match as much text as possible, while non-greedy matching tries to match as little text as possible. This can be important when dealing with patterns that contain repeating elements, as it can affect the overall behavior of the regex.

// Greedy matching example
$string = "This is a test string";
preg_match('/T.*g/', $string, $matches);
print_r($matches); // Output: Array ( [0] => This is a test string )

// Non-greedy matching example
$string = "This is a test string";
preg_match('/T.*?g/', $string, $matches);
print_r($matches); // Output: Array ( [0] => This is a test )