简体   繁体   English

正则表达式匹配模式与java中的子域

[英]Regex to match pattern with subdomain in java

I am trying to match the sub domain and domain. 我正在尝试匹配子域和域。 i used this code. 我用这个代码。

public static void main(String[] args) {
        String s = "http://aaa.example.com";
        Pattern pattern = Pattern.compile("http://([a-z0-9]*.)example.com");
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            System.out.println("match");
        }

    }

this works for http://aaa.example.com and also for http://aaa.example.commm . 这适用于http://aaa.example.com以及http://aaa.example.commm need to match only the sub domain for example.com UPDATE : Thanks for the answers and it works now i am facing another issue this regex is not matching http://example.com 需要匹配example.com更新的子域名:感谢您的答案,它现在有效,我正面临另一个问题,这个正则表达式不匹配http://example.com

Use matcher.matches() instead of find(). 使用matcher.matches()而不是find()。 Matches will match the entire region against the pattern. 匹配将使整个区域与模式匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM