How can negative look-behind and negative look-ahead assertions be used to improve the accuracy of extracting IP addresses from a text in PHP?

When extracting IP addresses from a text in PHP, negative look-behind and negative look-ahead assertions can be used to ensure that the extracted IP addresses are not part of a larger string or surrounded by unwanted characters. By using these assertions, we can accurately match IP addresses without including any surrounding text or characters.

$text = "The server's IP address is 192.168.1.1 and it can be accessed at that IP.";
$pattern = '/(?<!\d)(?:\d{1,3}\.){3}\d{1,3}(?!\d)/';

preg_match_all($pattern, $text, $matches);

$ip_addresses = $matches[0];

print_r($ip_addresses);