在使用正则表达式时,我使用 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;
}
}
}
有人知道我做错了什么吗?