Solved

Identify web link in a text

Posted on 2011-09-06
1
370 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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