Solved

Identify web link in a text

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 72
withoutTen challenge 14 88
count7 challenge 12 69
word0 challenge 3 58
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now