怎样判断一个字符串是否是网络地址(200分)

  • 主题发起人 主题发起人 程胜
  • 开始时间 开始时间

程胜

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样才能判断一个字符串究竟是程序呢,还是网络地址。
 
要看你怎么判断了,是单从语法上判断呢,还是判断改网址是否存在?
不过事实上本地的任何文件(包括程序)也可以表示为网址的形式,
遇到这种情况又怎么办呢?那就要看你的需求了。
 
程序是指什么,网络地址指的是什么(FTP?HTTP?OTHER?)
 
现在有很多垃圾网站会把自己加到注册表里,启动计算机后自动访问它。
我想知道用程序怎么判断,注册表里哪些是启动后自动运行的程序,哪些是自动访问的网站!
 
这是我找到的一个判断是否为有效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;
 
>>现在有很多垃圾网站会把自己加到注册表里,启动计算机后自动访问它。
>>我想知道用程序怎么判断,注册表里哪些是启动后自动运行的程序,哪些是自动访问的网站!
这样很简单呀,在那些网站在注册表写入的只是一个网址,不是本地程序,网址是http://开头的

 
Writer
网址可以不用加http://的
 
你的这个字符串是从哪里读出来的?
 
beta
当然是从注册表读的
 
从注册表当中读出数据以后你也可以通过检查文件是否存在原函数来验证呀。
不过最好的就是把数据获取了以后用pos来看看http,ftp,...,www,.com,.cn...是数据当中是否存在吗。
不知道你的意思是不是这样的呢?这个问题很好解决的吗。
 
同意楼上的看法
 
接受答案了.
 

Similar threads

回复
0
查看
848
不得闲
D
回复
0
查看
909
DelphiTeacher的专栏
D
D
回复
0
查看
704
DelphiTeacher的专栏
D
后退
顶部