ในกรณีนี้ เราใช้ re module ใน Python ที่นี่เรายอมรับ string และตรวจสอบว่า string มี ant URL อยู่ในนั้นหรือไม่ หาก 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.
โค้ดตัวอย่าง
# Program to find the URL from an input string
import re
def url(str):
# findall() has been used
# with valid conditions for urls in string
ur = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str)
return ur
# Driver Code
str = 'https://auth.mywebsite.org / user / python program / http://www.mywebsite.org/'
print("Url is :: ", url(str)) ผลลัพธ์
Url is :: ['https://auth.mywebsite.org / user / python program / http://www.mywebsite.org/']