```python
import re

def is_valid_URL(str):
    """
    Checks if a given string is a valid URL using a regular expression.
    """
    # Ensure the input is a string, as other types are not valid URLs.
    if not isinstance(str, str):
        return False

    # A comprehensive regex for validating a URL, structured for readability
    # using the re.VERBOSE flag, which allows for whitespace and comments.
    # This pattern is adapted from the Django URL validator.
    url_pattern = re.compile(
        r"""
        ^
        # Scheme: http, https, ftp, or ftps
        (?:http|ftp)s?://
        # Host: domain name, localhost, or IP address
        (?:
            # Domain name: subdomains and a top-level domain
            (?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+
            (?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)
            |
            # or localhost
            localhost
            |
            # or an IPv4 address
            \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
        )
        # Port (optional)
        (?::\d+)?
        # Path, query, and fragment (optional)
        (?:/?|[/?]\S+)?
        $
        """, re.VERBOSE | re.IGNORECASE)

    # The re.match function checks for a match only at the beginning of the string.
    # The `is not None` expression converts the match object (or None) to a boolean.
    return url_pattern.match(str) is not None
```