Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

JAVA檢查網址格式(RE)

如果要運用regular expression

來寫JAVA的檢查網址格式

http://www.yahoo.com.tw:2700/news/arl/palq.html%E7...

(有host port path 和resource)

就是在我輸入有host port path 和resource的網址後

就能判定該網址格式正確

該怎麼寫呢?

(怎麼湊都湊不出正確的@@")

1 Answer

Rating
  • 七喜
    Lv 5
    1 decade ago
    Favorite Answer

    @@"~跟偶棉下禮拜作業粉像耶只是我門還多了要解析的步驟

    import java.util.*;

    public class url {

    public static void main(String[] args) {

    Scanner cin=new Scanner(System.in);

    String urll=""; urll=cin.next();

    String regex ="http://[a-z0-9]{2,5}\\.[a-z0-9]{2,10}\\.((edu)|(gov)|(com)|(mil)|(net)|(org))\\.[a-z]{2}(:)[0-9]{2,4}/(.*)"; //網址規則式

    String host="", port="", path="", resource="" ;

    if (urll.matches(regex)) {

    String[] str=urll.split("//");

    str=str[1].split(":"); host=str[0];

    str=str[1].split("/"); port=str[0]; resource=str[str.length-1];

    for(int i=1;i<str.length-1;i++) path=path+"/"+str[i];

    System.out.println("host="+host);

    System.out.println("port="+port);

    System.out.println("path="+path);

    System.out.println("resource="+resource);

    } else{ System.out.println("不合"); }

    }

    }

    2007-11-09 07:43:20 補充:

    補充一個java規則式網頁給你玩..http://203.84.199.31/search/cache?p=java%E7%89%B9%...

    Source(s): wei, wei
Still have questions? Get your answers by asking now.