簡體   English   中英

如何在java中驗證允許使用通配符(*,%)的URL(域)

[英]how can I validate URL(domain) allowing wildcard(*, %) in java

我想在java中檢查驗證URL是否允許使用通配符。

我找到了一些關於驗證java中的URL的好例子( REGEXurlValidator ),但是那些沒有提供通配符。

這是我正在練習的內容:

CODE(urlValidator)

public void urlValidiTest(){
    System.out.println(this.urlCheck("https://www.google.com"));
    System.out.println(this.urlCheck("https://google.com"));
    System.out.println(this.urlCheck("*.com"));
}

public boolean urlCheck(String url){
    return new UrlValidator().isValid(url);
}

OUTPUT

真正

真正

CODE(正則表達式)

public void regexTest() {
  String[] URLs = new String[] { "http://www.google.com", "http://google.com/","*.com" };
    Pattern REGEX = Pattern.compile("(?i)^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$");
    for (String url : URLs) {
        Matcher matcher = REGEX.matcher(url);
        if (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

結果

http://www.google.com

http://google.com/

我想要做的是以上所有URL都有效。

我該如何解決這個問題呢?

任何評論將不勝感激。 謝謝。

更新

我擺脫了方案部分,並在答案之后的域部分添加了| *和| \\。*(| *和|。*給了我一個錯誤 - 無效的轉義序列(有效的是\\ b \\ t \\ n \\ f \\ r \\ n \\“\\') - 但我不確定更改是否正確)。

現在它不允許“google.com”; 但允許其他人(“www.google.com”,“google.com”,“。google.com”,“。com”)

 public void regexValidator(String str){

    Pattern REGEX = Pattern.compile(""
            + "(?i)^(?:\\S+(?::\\S*)?@)"
            + "?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)"
            + "(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
            + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|"

            //DOMAIN
            + "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+|\\*)"
            + "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*"
            //

            + "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)"
            + "(?::\\d{2,5})?(?:[/?#]\\S*)?$");

    Matcher _matcher = REGEX.matcher(str);
    if(_matcher.find()){
        System.out.println("[O] " + str);
    }
    else {
        System.out.println("[X]" + str);
    }
}

public void validate(){
    System.out.println("TEST START");
    this.regexValidator("https://www.google.com");
    this.regexValidator("www.google.com");
    this.regexValidator("google.com");
    this.regexValidator("*.google.com");
    this.regexValidator("*.com");
    System.out.println("DONE");
}

測試開始

[X] https://www.google.com

[O] www.google.com

[O] google.com

[O] * .google.com

[O] * .com

DONE

需要幫助嗎。 謝謝。

帶上一粒鹽,我現在無法訪問Java並從頭頂做到這一點,所以如果這里有錯誤,請隨時糾正我。

您需要更新正則表達式以包含通配符。 考慮到這件事情有多復雜,這不是微不足道的。

讓我們首先打破你的正則表達式:

(?i)
^
    (?:
        (?:
            https?|ftp
        )
        ://
    )
    (?:
        \S+
        (?:
            :\S*
        )?
        @
    )?
    (?:
        (?!
            (?:
                10|127
            )
            (?:
                \.\d{1,3}
            ){3}
        )
        (?!
            (?:
                169\.254|192\.168
            )
            (?:
                \.\d{1,3}
            ){2}
        )
        (?!
            172\.
            (?:
                1[6-9]|2\d|3[0-1]
            )
            (?:
                \.\d{1,3}
            ){2}
        )
        (?:
            [1-9]\d?|1\d\d|2[01]\d|22[0-3]
        )
        (?:
            \.
            (?:
                1?\d{1,2}|2[0-4]\d|25[0-5]
            )
        ){2}
        (?:
            \.
            (?:
                [1-9]\d?|1\d\d|2[0-4]\d|25[0-4]
            )
        )
        |
        (?:
            (?:
                [a-z\u00a1-\uffff0-9]-*
            )*
            [a-z\u00a1-\uffff0-9]+
        )
        (?:
            \.
            (?:
                [a-z\u00a1-\uffff0-9]-*
            )*
            [a-z\u00a1-\uffff0-9]+
        )*
        (?:
            \.
            (?:
                [a-z\u00a1-\uffff]{2,}
            )
        )
        \.?
    )
    (?:
        :\d{2,5}
    )?
    (?:
        [/?#]\S*
    )?
$

我們現在可以看到該方案有組,用戶名/密碼對(具有@字符的組),域本身的大組,端口組和可能的路徑,查詢或片段部分。 大組可以分為兩部分(由| (OR)分隔),第一部分用於IP地址,負向預見不允許本地IP,后者用於命名域,由一個或多個部分組成通過一個點,最后是頂級域名。

那么你需要做些什么才能允許使用通配符? 在要允許替換為通配符的每個組中添加通配符( *% ):

如果要為方案允許通配符,請在此處添加一個:

    (?:
        (?:
            https?|ftp
            |\*    <-----
        )
        ://
    )

如果你想允許用戶名和/或密碼部分的通配符,你不需要做任何事情,你的正則表達式已經允許任何非空白字符,所以*:*@*@已經有效。

如果要允許域名使用通配符,請在此處添加:

        (?:
            (?:
                [a-z\u00a1-\uffff0-9]-*
            )*
            [a-z\u00a1-\uffff0-9]+
            |\*    <-----
        )
        (?:
            \.
            (?:
                [a-z\u00a1-\uffff0-9]-*
            )*
            [a-z\u00a1-\uffff0-9]+
            |\.\*    <-----
        )*

如果您想允許TLD使用通配符,請在此處添加:

        (?:
            \.
            (?:
                [a-z\u00a1-\uffff]{2,}
                |\*    <-----
            )
        )

如果要為端口允許通配符,請在此處添加一個:

    (?:
        :\d{2,5}
        |:\*    <-----
    )?

如果你想允許路徑使用通配符,你不需要做任何事情,你的正則表達式已經涵蓋( /*/*/*/foobar等已經有效)。

最后,但並非最不重要的是,如果您想要將方案和域名的通配符放在一起 (例如在您的示例中),則需要添加一個新組並將其添加到:

    |
    (?:
        \*
        \.
        (?:
            [a-z\u00a1-\uffff]{2,}
        )
    )
    (?:
        :\d{2,5}
    )?
    (?:
        [/?#]\S*
    )?

基本上只是添加在最后一組后面和$符號之前。 如果您願意,也不要忘記在這里添加通配符到TLD和/或端口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM