Pattern pattern = Pattern.compile("<(\\S+?)\\s+(?:\"[^\"]*\"|'[^']*'|[^\"'><])+>(.+?)</\\1>"); Matcher matcher = pattern.matcher("<a href=\"http://baidu.com\">这是连接文字 </a><a onclick=\"alert(1)\" href=\"http://baidu.com\">这是连接文字2</a>"); while(matcher.find()) { if(matcher.groupCount() > 1) System.out.println(matcher.group(2)); }