正则表达式提取特定内容
在使用正则表达式时,有时我们需要过滤出特定内容。本文将介绍如何使用正则表达式从 html 代码中提取含有特定字符的链接。
问题:
如何从以下 html 代码中提取所有以 "www." 开头并以 ".com" 或 ".cn" 结尾的链接?
立即学习“前端免费学习笔记(深入)”;
<p>++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++</p>
答案:
import java.util.regex.matcher; import java.util.regex.pattern; public class linkextractor { public static void main(string[] args) { string html = "<a>www.baidu.com</a><a>www.qq.com</a><a>www.aaa.cn</a>www.eee.cn"; string reg = "www.*?(com|cn)"; matcher matcher = pattern.compile(reg, pattern.case_insensitive).matcher(html); while (matcher.find()) { system.out.println(matcher.group()); } } }
匹配结果:
www.baidu.com www.qq.com www.aaa.cn www.eee.cn
解析:
以上就是如何用正则表达式从HTML代码中提取特定类型的链接?的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号