我在使用正则表达式时使用RegexBuddy。在其库中,我复制了正则表达式以匹配URL。我在RegexBuddy中成功测试。但是,当我复制它作为Java字符串风格和粘贴到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;
}
}
}
有人知道我在做什么错了吗?