
import re
def match_url(string):
pattern = r"^(http|https|ftp)://[a-zA-Z0-9]+\.[a-zA-Z0-9]+(:[0-9]+)?(/[a-zA-Z0-9]+)*(\?[a-zA-Z0-9]+=[a-zA-Z0-9]+)*(#[a-zA-Z0-9]+)?$"
match = re.match(pattern, string)
if match:
return True
else:
return False
string = "http://www.example.com/path/to/page.html?param=value#anchor"
print(match_url(string)) # True
string = "https://www.example.com:8080/path/to/page.html?param=value#anchor"
print(match_url(string)) # True
string = "ftp://www.example.com/path/to/file.txt"
print(match_url(string)) # True直接头皮发麻,不要慌,我们逐个解释