相关疑难解决方法(0)

用于匹配Java中的URL的正则表达式

我在使用正则表达式时使用RegexBuddy.我从它的库中复制了正则表达式以匹配URL.我在RegexBuddy中成功测试过.但是,当我将其复制为Java String风格并将其粘贴到Java代码中时,它不起作用.以下类打印false:

public class RegexFoo {

    public static void main(String[] args) {
        String regex = "\\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]";
        String text = "http://google.com";
        System.out.println(IsMatch(text,regex));
}

    private static boolean IsMatch(String s, String pattern) {
        try {
            Pattern patt = Pattern.compile(pattern);
            Matcher matcher = patt.matcher(s);
            return matcher.matches();
        } catch (RuntimeException e) {
        return false;
    }       
}   
}
Run Code Online (Sandbox Code Playgroud)

有谁知道我做错了什么?

java regex regexbuddy

73
推荐指数
4
解决办法
17万
查看次数

标签 统计

java ×1

regex ×1

regexbuddy ×1