LintCode Url Parser
原题网址:http://www.lintcode.com/en/problem/url-parser/
Parse a html page, extract the Urls in it.
Hint: use regex to parse html.
Given the following html page:
<html><body><div><a href="http://www.google.com" class="text-lg">Google</a><a href="http://www.facebook.com" style="display:none">Facebook</a></div><div><a href="https://www.linkedin.com">Linkedin</a><a href = "http://github.io">LintCode</a></div></body>
</html>
You should return the Urls in it:
["http://www.google.com","http://www.facebook.com","https://www.linkedin.com","http://github.io"
]
方法:正则表达式,重点是各种奇葩的情况。
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class HtmlParser {// Pattern pattern1 = Pattern.compile("(href\\s*=\\s*\")([^\"]*?)(\")", Pattern.CASE_INSENSITIVE);// Pattern pattern2 = Pattern.compile("(href\\s*=\\s*')([^']*?)(')", Pattern.CASE_INSENSITIVE);Pattern pattern = Pattern.compile("(href\\s*=\\s*[\"']?)([^\"'\\s>]*)([\"'>\\s])", Pattern.CASE_INSENSITIVE);/*** @param content source code* @return a list of links*/public List<String> parseUrls(String content) {// Write your code hereList<String> results = new ArrayList<>();Matcher matcher = pattern.matcher(content);match(matcher, results);return results;}private void match(Matcher matcher, List<String> results) {while (matcher.find()) {String url = matcher.group(2);if (url.length() == 0 || url.startsWith("#")) continue;results.add(url);}}
}
LintCode Url Parser相关推荐
- JS URL Parser
为什么80%的码农都做不了架构师?>>> /** *@param {string} url 完整的URL地址 *@returns {object} 自定义的对象 *@descr ...
- URL deconding URL解码
This task (the reverse of URL encoding and distinct from URL parser) is to provide a funct ...
- [转]Passing data between pages in JQuery Mobile mobile.changePage
本文转自:http://ramkulkarni.com/blog/passing-data-between-pages-in-jquery-mobile/ I am working on a JQue ...
- 超强1000个jquery极品插件!(连载中。。。。最近更新20090710)
花N长时间积累的Jquery插件,希望大家喜欢.大家还有什么新的插件,请留言,我们一并收录. 感谢大家的支持. ++++++++++++++++++++++++++++++++++++++++++++ ...
- JBPM深入解析之变量设计
JBPM深入解析之变量设计 在流程的流转的过程中,很多时候我们需要根据不同的实际情况传入一些初始化数据,以便完成我们个性化的业务需求:同时很多时候我们需要在不同的节点之间共享一些业务数据,特别是一些节 ...
- XML专题:使用NSXMLParser解析xml文件
使用NSXMLParser解析xml文件 1. 设置委托对象,开始解析 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dat ...
- 用到lucene的爬虫的简单实现
2019独角兽企业重金招聘Python工程师标准>>> 小菜鸟我最近研究了一下lucene,以及前面的爬虫的写法,我想到能否用lucene写一个站内搜索,由于我对htmlprase不 ...
- jQuery:收集一些基于jQuery框架开发的控件/jquery插件2
Horizontal accordion: jQuery 基于jQuery开发,非常简单的水平方向折叠控件. 主页:http://letmehaveblog.blogspot.com/2007/10/ ...
- python爬虫简单实例-Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站"http://bbs.51tes ...
最新文章
- 图片浏览器每次只能打开一张图片_那些你可能不知道的浏览器奇技淫巧
- 玩“剪刀石头布“的脑机!密歇根大学开发由大脑意识精密控制的假肢
- 【Netty】mmap 和 sendFile 零拷贝原理
- 【Ubuntu】 Ubuntu16.04搭建NFS服务
- 给APK签名,修改签名
- MATLAB double、str、cell间的类型转换
- if ($this-getRequest()-isPost()) {
- 三年研发、数亿美元成本,Mate 20的“大杀器”麒麟980是怎样炼成的?
- 替代left join方法_你应该使用pathlib替代os.path
- 何为计算机视觉?计算机视觉与数字图像处理的区别、Opencv的起源。
- 利用 Web Share API 将网页分享到 App(下)
- 八、IO优化(7)减少IO竞争
- T100服务端接口开发步骤
- fseek 函数(易懂)
- 计算机中找计算器二进制转换咋找,计算器自带二进制转换器,你值得学习
- 转: Github上关于iOS的各种开源项目集合
- 使用FFmpeg进行mp4与m3u8之间转换
- Luat模块应用手册-示例-Luat DEMO-长连接超低功耗方案
- 2015年医学核心期刊目录(有效期自2014年)
- iOS 视频直播技术