問題地址:/question/question/317437658 . html
詳細解釋:
標準URL格式如下:
這壹點不能省略。
[4]端口是壹個端口。可以省略,壹般默認為80。
[5] myapp1,myapp2是應用目錄。省略
index.html是當前頁面,可以省略,默認為index . html、default.html等。
[6] Key1、Value1、Key2和Value2是傳遞給網站的參數,可以省略。
[7]錨點是網頁中的錨點。
法律網站示例:
www.baidu.com
:80/
/question/311110938 . html?an = 0 & ampsi=1
判斷網址的算法:
最簡單的當然是正則表達式。
我們看到標準網址(URL)的七個部分帶有六個分隔符,即:
://
@
:
/
?
#
在瀏覽器中,只有第三部分必須填寫,這是我們每天寫得最多的。
使用的資源文件:
頭文件deelx.h可以從/zh/deelx/下載
下載並將其放在與源程序相同的目錄中。
*/
# include & ltstdio.h & gt
#包含“deelx.h”
int main(int argc,char * argv[])
{
char text[1000]= ":800/myapp 1/myapp 2/index . html?key 1 = value 1 & amp;key 2 = value 2 # anchor ";
//判斷合法網站的正則表達式
靜態CRegexpT & ltchar & gt正則表達式
(
"((([:alpha:]+)\\://)?((\\w+)\\:(\\w+)@)?(\\w+(\\。\\w+)*)(\\:(\\d+))?((/\\w+)*(\\。\\w+)?)?(\\?((\ \ w+= \ \ w+& amp;)*(\\w+=\\w+)))?(#((\\w+)))?)"
);
int I;
Printf("輸入壹個URL:");
while(scanf("%s ",text))
{
MatchResult結果= regexp。匹配(文本);
如果(結果。IsMatched())
{
/*
for(I = 0;我& lt結果。maxgroup number();i++)
{
printf("%d=%。*s\n ",I,結果。GetGroupEnd(i) -結果。GetGroupStart(i),文本+結果。GetGroupStart(I));
}
*/
//解析URL
Printf("網址:%。* s \ n ",結果。getgroupend(0)-結果。getgroupstart (0),文本+結果。getgroupstart(0));
Printf("協議類型:%。* s \ n ",result . getgroupend(2)-result . getgroupstart(2),text+result . getgroupstart(2));
Printf("用戶名:%。* s \ n ",result . getgroupend(4)-result . getgroupstart(4),text+result . getgroupstart(4));
Printf("用戶密碼:%。* s \ n ",result . getgroupend(5)-result . getgroupstart(5),text+result . getgroupstart(5));
Printf("服務器IP或域名:%。* s \ n ",result . getgroupend(6)-result . getgroupstart(6),text+result . getgroupstart(6));
Printf("服務器端口:%。* s \ n ",result . getgroupend(9)-result . getgroupstart(9),text+result . getgroupstart(9));
Printf("應用程序目錄:%。* s \ n ",result . getgroupend(10)-result . getgroupstart(10),text+result . getgroupstart(10));
Printf("查詢參數:%。* s \ n ",result . getgroupend(14)-result . getgroupstart(14),text+result . getgroupstart(14));
Printf("錨:%。* s \ n \ n ",result . getgroupend(18)-result . getgroup start(18),text+result . getgroup start(18));
}
其他
{
printf(" % s "是非法網站!!!\n ",文本);
}
Printf("輸入壹個URL:");
}
返回0;
}
/*
運行結果:
輸入網址::800/myapp 1/myapp 2/index . html?key 1 = value 1 & amp;key2 = value2 #錨
網址::800/myapp 1/myapp 2/index . html?key 1 = value 1 & amp;key2 = value2 #錨
協議類型:
服務器端口:800
應用程序目錄:/myapp 1/myapp 2/index . html。
查詢參數:key 1 = value 1 &;key2 =值2
錨:錨
*/