I am trying to find a clean way to extract all urls in a text string.
After an extensive search, i have found many posts suggesting using regular expressions to do the task and they give the regular expressions that suppose to do that. Each of the RegExs have some advantages and some short comings. Also, editing them to change their behaviour is not straight forward. Anyway at this point i am happy with any RegEx that could detect the urls in this text correctly:
Input:
Lorem ipsum dolor sit amet https://www.lorem.com/ipsum.php?q=suas, nusquam tincidunt ex per, ius modus integre no, quando utroque placerat qui no. Mea conclusionemque vituperatoribus et, omnes malorum est id, pri omnes atomorum expetenda ex. Elit pertinacia no eos, nonumy comprehensam id mei. Ei eum maiestatis quaerendum https://www.lorem.org
Using an existing library is probably the best solution.
But it was too much for my tiny script, and -- inspired by @piotr-wasilewiczs answer-- I came up with:
A bit too dense for my taste and I have no clue how fast it is, but it should detect most "sane" urls in a string.
If you want a regex, you can use this:
Apart from what others mentioned, since you've asked for something that already exists, you might want to try URLExtract.
Apparently it tries to find any occurrence of TLD in given text. If TLD is found, it starts from that position to expand boundaries to both sides searching for a "stop character" (usually white space, comma, single or double quote).
You have a couple of examples here.
It seems that this module also has an
update()
method which lets you update the TLD list cache fileHowever, if that doesn't fit you specific requirements, you can manually do some checks after you've processed the urls using the above module (or any other way of parsing the URLs). For example, say you get a list of the URLs:
You can then build another lists which hold the excluded domains / TLDs / etc:
your example: http://ideone.com/wys57x
After all you can also cut last character in elements of list if it is not a letter.
EDIT:
Your example: http://ideone.com/gHRQ8w