Solved

Extracting web links from string

Posted on 2004-04-24
8
337 Views
Last Modified: 2010-04-05
Hello,

How can I extract web links from string, for example I have string with web links and other text and I need only those web links that match special code or text.
0
Comment
Question by:H-styler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10907060

I advise regular expressions.

Freeware for Delphi
http://regexpstudio.com/TRegExpr/TRegExpr.html
0
 
LVL 17

Expert Comment

by:mokule
ID: 10907068

This is an example of regular expression from TRegExpr test program for extracting URL from text.

(?i)                          # we need caseInsensitive mode
(FTP|HTTP)://                 # protocol
([_a-z\d\-]+(\.[_a-z\d\-]+)+) # TCP addr
((/[ _a-z\d\-\\\.]+)+)*       # unix path
0
 
LVL 3

Accepted Solution

by:
Kunfufaresi earned 50 total points
ID: 10907298
Hello, not as fancy as regex but you could use this code

s:String;
i :integer;

begin
 s := texttoparse;
 repeat
  i := pos('http://',s);
  if i>0 then
  begin
   delete(s,1,i-1);
   i := pos(' ');
   if i=0 then i := length(s)+1;
   listbox1.items.add(copy(s,1,i-1)); // so the url is = copy(s,1,i-1)
   delete(s,1,i);
  end;
 until i=0;
end;

well this would work if your urls doesnt contain spaces which they shouldnt as spaces are encoded already.

Kunfu Faresi
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:mokule
ID: 10907314
I advise regexpr because he wrote
"I need only those web links that match special code or text."
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10907605
Yes you are right, regexp is much more advanced, but i've used it some times and still have not gotten all the hang of it.
0
 

Author Comment

by:H-styler
ID: 10908088
Kunfufaresi with your code I can`t extract links from this type of string:

<table border=0 cellpadding=0 cellspacing=0 bgcolor=ffffff><tr><td><a href="http://www.bythebeachemails.com/scripts/runner.php?PA=116" target=_ptc onclick="javascript:reloadpage(30)"><img src=http://www.bythebeachemails.com/scripts/runner.php?REDIRECT=http%3A%2F%2Fmpam2.free.fr%2Fcash%2Fbann_cash2.gif alt="Great PTR/PTC sites" width=468 height=60 border=0></a></td></tr></table>The ad above is worth 50 cent(s)<br><br><br><br><br><b>1</b><br> <br>

And I only need to extract those links witch match for example this code: "scripts/runner.php?PA="
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10908442
Hello,

well if you did a

if pos('scripts/runner.php?PA=',copy(s,1,i-1))>0 then listbox1.items.add(copy(s,1,i-1));

also you should in this case look for pos('"',s) instead of pos(' ',s) as " terminates the url not space.
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 10932036
As mokule said i suggest you to use regular expressions. Here is some usefull stuff http://www.regular-expressions.info/ and if you will search for regular expressions in www.torry.ru, you may found some freeware components.

Regards,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month4 days, 23 hours left to enroll

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question