Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Identify web link in a text

Posted on 2011-09-06
1
Medium Priority
?
375 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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