Solved

Identify web link in a text

Posted on 2011-09-06
1
371 Views
Last Modified: 2012-06-21
Hi Experts,

Well, I need to do something to identify web links in a text using delphi xe.
For example, if I have a text like this:

"Hi, please visite my web sites: www.myweb1.com , myweb2.com"

I need to do a function to get only the web links (2 in this case)

Some idea
Thanks
0
Comment
Question by:alpires
[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
1 Comment
 
LVL 12

Accepted Solution

by:
Hypo earned 500 total points
ID: 36492518
Yeah,
use regular expressions to get a list of matches for an URL-pattern... (see the ListWebLinks example below)...

I got the regular expression in the ListWebLinks by googling... you might want to tweak it depending on how accurate you need the results to be...

/Hypo
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RegularExpressions;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    procedure Memo1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure ListWebLinks(AText : TStrings; ALinks : TStrings);
var i : integer;
    aRegEx : TRegEx;
    aMatches : TMatchCollection;
begin
  aRegEx := TRegEx.Create('((http|ftp|https):\/\/)?[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?');
  aMatches := aRegEx.Matches(AText.Text);
  for i := 0 to aMatches.Count-1 do
    ALinks.Add(aMatches.Item[i].Value);
end;

procedure TForm1.Memo1Change(Sender: TObject);
begin
  Memo2.Lines.Clear;
  ListWebLinks(Memo1.Lines, Memo2.Lines);
end;

end.

Open in new window

0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

751 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