View Full Version : Extracting a link from block of text
dgwillia
11-17-2009, 03:50 PM
I would like to extract a link from a block of text. The text gets passed in by a HTTPService call and the I loop throuhg each object to disply the text of each object as row within a table.
However the text almost always contains a url in, so I would like to extract the link within the block of text and display within another column.
For example, the following text shows a typical response from the HTTPService..
Loch Fyne Whiskies announces bottling of 2010 http://tinyurl.com/yak5yrc
I would like to extract the link from the above text.. any ideas?
Thanks
marco.rossi
12-11-2009, 05:10 PM
Hi,
You can use a regular expression.
Peter Cowling
12-12-2009, 01:03 PM
Along these lines:
/(?<protocol>http|ftp|https)://(?<domain>[^/\r\n]+)(?<path>/+[a-zA-Z0-9~#%@\&:=?\/\.,_-]+\.+[a-zA-Z]{2,4})?/
Leading or lagging text will be stripped. It does not check the validity of the top level domain i.e. .com, thereby accomodating, for example, .aspx etc.
Barna Biro
12-12-2009, 01:18 PM
A more simplified version of the above would be:
/(?<protocol>http|ftp)://(?<domain>[^/\r\n]+)(?<path>/[^\r\n]*)?/
Also, it would be worth bookmarking this: http://ryanswanson.com/regexp/#start You can run test directly with that application and you also have a quite sweet little library with expression that you can use "out of the box".
Best wishes,
Barni
Peter Cowling
12-12-2009, 01:52 PM
A more simplified version of the above would be:
/(?<protocol>http|ftp)://(?<domain>[^/\r\n]+)(?<path>/[^\r\n]*)?/
Also, it would be worth bookmarking this: http://ryanswanson.com/regexp/#start You can run test directly with that application and you also have a quite sweet little library with expression that you can use "out of the box".
Best wishes,
Barni
Note that lagging text is not trimmed i.e. okay for the example provided, but not in general.
Barna Biro
12-12-2009, 02:14 PM
As I have pointed out, it's "a more simplified version". ;)
Peter Cowling
12-12-2009, 02:39 PM
Simplification depends on functional equivalence.
You have simplified the request 'extract a link from a block of text', based on the inference that the url will always be placed at the end of the text, and provided an example accordingly.
I don't argue the case either way, but the distinction is worth noting.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.