Skip to content
Advertisement

How to find a link in a big string

I want to find a link like https://stackoverflow.com/questions/37543724/python-regex-for-finding-all-words-in-a-string

in a big string but there are many links and I want all links that starts with https://stackoverflow.com/questions/ the string look like

something https://stackoverflow.com/questions/37543724/python-regex-for-finding-all-words-in-a-string something

So my question is how can i find an uncompletet string?

Advertisement

Answer

Try this:

import re
  
def Find(string):
  
    # findall() has been used 
    # with valid conditions for urls in string
    regex = r"(?i)b((?:https?://|wwwd{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^s()<>]+|(([^s()<>]+|(([^s()<>]+)))*))+(?:(([^s()<>]+|(([^s()<>]+)))*)|[^s`!()[]{};:'".,<>?«»“”‘’]))"
    url = re.findall(regex,string)      
    return [x[0] for x in url if "stackoverflow.com/questions" in x[0]]
      
# Driver Code
string = 'content_license CC BY-SA 4.0 link https//stackoverflow.com/questions/26325943/many-threads-to-write-log-file-at-same-time-in-python title Many threads to writ'
print(Find(string))

This outputs: ['stackoverflow.com/questions/26325943/many-threads-to-write-log-file-at-same-time-in-python'], which is what I assume you want.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement