Python | Check url in line

File handling | Python Methods and Functions | Regular Expressions

In this article, we will need to accept a string and check if the string contains any url. If the url is present in the string, we will say the url is found or not and print the corresponding url present in the string. We'll use Python's regex concept to solve the problem.

Examples:

 Input: string = 'My Profile: https://auth.python.engineering/user/Chinmoy%20Lenka / articles in the portal of http:// www.python.engineering / 'Output: URLs: [' https:// auth.python.engineering / user / Chinmoy% 20Lenka / articles', 'http://www.python.engineering / '] Input: string =' I am a blogger at https:// python.engineering' Output: URL: ['https:// python.engineering'] 

To find a URL in a given string, we used the

# Python code to find URL from input string
# Using regular expression

import re

 

def Find (string):

# findall () was used

# with valid URL conditions in line

  url = re.findall ('http [s] ?: / / (?: [a - zA - Z] | [ 0 - 9 ] | [$ - _ @. & amp; + ]

| [! * (),] | (?: % [ 0 - 9a - fA - F] [ 0 - 9a - fA - F ])) + ', string)

return url

  
Driver code

string = ' My Profile: https: / / auth.python.engineering

/ user / Chinmoy % 20Lenka / articles in  

the portal of http: / / www.python.engineering / '

print ( " Urls: " , Find (string))

Output:

 Urls: ['https:// auth.python.engineering / user / Chinmoy% 20Lenka / articles',' http://www.pythonengineering. org / '] 




Get Solution for free from DataCamp guru