How can regular expressions (Regex) be used to extract specific information from a URL in PHP?

To extract specific information from a URL in PHP using regular expressions (Regex), you can define a pattern that matches the desired part of the URL and then use the preg_match function to extract the information. For example, if you want to extract the domain name from a URL, you can create a Regex pattern that matches the domain name part of the URL and then use preg_match to extract it.

$url = "https://www.example.com/page";
$pattern = '/^(https?:\/\/)?([\w\.]+)\/.*/';
preg_match($pattern, $url, $matches);
$domain = $matches[2];

echo $domain; // Output: example.com