用于检查字符串中 URL 的 Python 程序
在这种情况下,我们在Python中使用re模块,这里我们接受一个字符串并检查该字符串中是否包含antURL。如果字符串中存在URL,则显示。我们使用findall()方法来解决这个问题。
算法
Step 1: given string as input. Step 2: findall() function is return all non-overlapping matches of pattern in string and in this function the string is scanned left to right and matches are returned in the order found.
示例代码
#从输入字符串中查找URL的程序 import re def url(str): #findall()已被使用 #具有字符串中url的有效条件 ur = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str) return ur #驱动程序代码 str = 'https://auth.mywebsite.org / user / python program / http://www.mywebsite.org/' print("网址是:: ", url(str))输出结果
网址是:: ['https://auth.mywebsite.org / user / python program / http://www.mywebsite.org/']