这篇文章主要给大家介绍了关于Java正则表达式如何匹配特定html标签内的内容的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
如题:
比如,对于如下文本串:
... ignored content
prefix content
inner content
postfix content
... ignored content
标签内的内容: inner content(这里的html标签可以换成任何其它的标签,比如
标签)
这里引入正则表达式的group概念:详细点击文章查看
比如:对于一个正则表达式( ( A ) ( B ( C ) ) )
group 1为:( ( A ) ( B ( C ) ) )
group 2为:( A )
group 3为:( B ( C ) )
group 4为:( C )
这样,我们就能够构造出如下的正则表达式:.*()(.*)2).*
此表达式的group概念为:
group 1: ()(.*)2)
group 2: (html>)
group 3: (.*)
显然我们要求的就是group3的内容。
注意:2是对group2的引用,也就是html>
该正则表达式也可以写成: .*()(.*)(html>)).* 化简其实就是.*(.*).*
代码实现为:
String p = ".*()(.*)\2).*";>午休abcpostfix"; System.out.println("Pattern: " + p); System.out.println("String to be test: " + m); Pattern pattern = Pattern.compile(p); Matcher matcher = pattern.matcher(m); if(matcher.matches()) { System.out.println("Matched String: " + matcher.group(3)); } else { System.out.println("So sad, not matching anything!"); }
总结
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。