Tag: regexbuddy

正则expression式来匹配Java中的URL

在使用正则expression式时,我使用了RegexBuddy。 从它的库中,我复制了正则expression式来匹配URL。 我在RegexBuddy中testing成功。 但是,当我将其作为Java String flavor复制并粘贴到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; […]