Are there any potential pitfalls to using regular expressions with preg_match to filter URLs in PHP?

One potential pitfall of using regular expressions with preg_match to filter URLs in PHP is that the regex pattern may not be comprehensive enough to accurately match all valid URLs. To solve this issue, it is recommended to use a more robust URL parsing library or function that is specifically designed for handling URLs, such as the parse_url function in PHP.

$url = "https://www.example.com";
if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "Valid URL";
} else {
    echo "Invalid URL";
}