这是我找到的一个判断是否为有效url的函数
function GetURLs(line: string): TStringList;
var
i : integer;
posURL,
s : string;
begin
//Obviously, first we create a TStringList.
Result := TStringList.Create;
//We must make a copy of our string, since we need a variant section of a string
//to work with out loop. S is just that.
s := line;
//This is our counter.
i := 0;
//If the string is smaller than 5 characters, its not going to be a URL, that is
//for sure.
if Length(s) < 5 then exit;
//We begin our repeat..unitl loop, using I as the counter of spaces.
//This is a classic string cracking loop, where a a sentence is broken into its words.
//Here, we are breaking the string into words, and checking each word in the string
//if it has any properties that qualify it to be a url.
repeat
//Get the first space.
i := Pos(' ', s);
//If a space does exist, in fact, then we carve out the possible link.
if i > 0 then
begin
//This is the copy method.
posURL := Copy(s, 1, i-1);
//Decrease the s, dynamic string, by whatever we just carved out, so next time
//we loop around, we dont have to worry about it. Just use copy to remove the
//value equivalent to posURL from the main string.
s := Copy(s, i+1, Length(s)-i);
//Call the function below, and check if the word is actually a link.
if isURL(posURL) then
//if the word indeed is a ling, then add it to the string list.
Result.Add(posURL);
//The expressions within this begin..end section do the same as above, but this
//is called if only one word/token remains in the s string.
end else
begin
PosURL := s;
if isURL(posURL) then
Result.Add(posURL);
Break;
end;
//if s is empty, stop the loop.
until Length(s) = 0;
//This little section will free the TStringList if it contains no items, and set it
//to nil.
if Result.Count = 0 then
begin
Result.Free;
Result := nil;
end;
end;
{ This function does the actual checking for the token being a URL. }
function IsURL(s: string): Boolean;
var
i: integer;
begin
//From the top, make the result false.
Result := False;
//Again, if the text of the token is less than 5, its just not going
//to be a URL.
if Length(s) < 5 then exit;
//If the token contains a period at the end, or two periods combined,
//its also not going to be a url.
if (s[Length(s)] = '.') or (Pos('..', s) > 0) then exit;
//Now we check for bogus characters, Anything between 33 and 126 is valid,
//Above or below, are not valid url characters.
for i := 1 to Length(s) do
if (Ord(s) < 33) or (Ord(s) > 126) then exit;
//The next few things check for individual characteristics of various stringd.
if (Pos('www.',LowerCase(s)) = 1) or (Pos('news:', LowerCase(s)) = 1) and
(Length(s) > 6) then
begin
Result := True;
Exit;
end;
if (Length(s) > 12) or (Pos('mailto:', LowerCase(s)) = 1) and
(Pos('@', s) > 1) and (Pos('.', s) > 4) and (Pos('.', s) > (Pos('@', s) +1)) then
begin
Result := True;
Exit;
end;
if (Pos('http:://', LowerCase(s)) > 0) or (Pos('ftp://', LowerCase(s)) > 0) and
(Length(s) > 10) and (Pos('.', s) > 7) then
begin
Result := True;
Exit;
end;
end;