2

I have a bot that stores a file on the cloud, then gives me a link to the file like so:

>upload sample.png
>>The link to your file is https://filecloud.com/12345/sample.png.html. If this link doesn't work, try https://filecloudlinks.com/12345/sample.png.html.

I would like to isolate the link (or both links) from the string. I tried with slice(), but since the length of the link can change, this won't work. How can I detect and slice the "https" until ".html"?

  • 2
    Does this answer your question? [How do you extract a url from a string using python?](https://stackoverflow.com/questions/9760588/how-do-you-extract-a-url-from-a-string-using-python) Regular expressions will do the trick. – rgov Apr 14 '20 at 01:24
  • 2
    first link - `text[ text.find("https") : text.find(".html")+5 ]`. For second link you would have to use `start = text.find("https") + 1` and then `find(..., start)`. OR use `regex` for this. – furas Apr 14 '20 at 01:25
  • @furas Your first method is much simpler than using regex, Thanks! – user13306848 Apr 14 '20 at 01:47
  • 1
    for first link you can also use `start = 0` and `find(..., start)` - this way you can use it in `while` loop to get more urls from text. When `find("https", start)` will give `-1` then there is no more urls - https://pastebin.com/GFEvTaGw. But it may gives wrong results if there is text `"https hello world .html"` – furas Apr 14 '20 at 01:56

0 Answers0