import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[] args){ Pattern p = Pattern.compile("((?=\\img.*(?=(http|https)))(http|https))"); Matcher matcher = p.matcher("img src="http://news.163.com/sports/yao.jpg" src="http://news.163.com/sports/yao.jpg"/>"); System.out.println(matcher.matches()); } }
程序本身應該沒有什么問題,但是運行結(jié)果: Exception in thread "main" java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 27 ((?=\img.*(?=(http|https)))(http|https)) 我Google了半天,最后得到的唯一結(jié)果就是換種寫法。希望看到這篇文章的朋友,如果已經(jīng)實現(xiàn)了這種效果,一定要留言不吝賜教。 另外,在我看文檔時看到了?:引導的正則表達式,上面說是“匹配不存儲”,百思不得其解。感覺還是代碼來的實在,寫一個看一下: