原题网址:http://www.lintcode.com/en/problem/url-parser/

Parse a html page, extract the Urls in it.

Hint: use regex to parse html.

Have you met this question in a real interview?

Yes

Example

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相关推荐

  1. JS URL Parser

    为什么80%的码农都做不了架构师?>>>    /** *@param {string} url 完整的URL地址 *@returns {object} 自定义的对象 *@descr ...

  2. URL deconding URL解码

    This task   (the reverse of   URL encoding   and distinct from   URL parser)   is to provide a funct ...

  3. [转]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 ...

  4. 超强1000个jquery极品插件!(连载中。。。。最近更新20090710)

    花N长时间积累的Jquery插件,希望大家喜欢.大家还有什么新的插件,请留言,我们一并收录. 感谢大家的支持. ++++++++++++++++++++++++++++++++++++++++++++ ...

  5. JBPM深入解析之变量设计

    JBPM深入解析之变量设计 在流程的流转的过程中,很多时候我们需要根据不同的实际情况传入一些初始化数据,以便完成我们个性化的业务需求:同时很多时候我们需要在不同的节点之间共享一些业务数据,特别是一些节 ...

  6. XML专题:使用NSXMLParser解析xml文件

    使用NSXMLParser解析xml文件 1. 设置委托对象,开始解析      NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dat ...

  7. 用到lucene的爬虫的简单实现

    2019独角兽企业重金招聘Python工程师标准>>> 小菜鸟我最近研究了一下lucene,以及前面的爬虫的写法,我想到能否用lucene写一个站内搜索,由于我对htmlprase不 ...

  8. jQuery:收集一些基于jQuery框架开发的控件/jquery插件2

    Horizontal accordion: jQuery 基于jQuery开发,非常简单的水平方向折叠控件. 主页:http://letmehaveblog.blogspot.com/2007/10/ ...

  9. python爬虫简单实例-Python 利用Python编写简单网络爬虫实例3

    利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站"http://bbs.51tes ...

最新文章

  1. 图片浏览器每次只能打开一张图片_那些你可能不知道的浏览器奇技淫巧
  2. 玩“剪刀石头布“的脑机!密歇根大学开发由大脑意识精密控制的假肢
  3. 【Netty】mmap 和 sendFile 零拷贝原理
  4. 【Ubuntu】 Ubuntu16.04搭建NFS服务
  5. 给APK签名,修改签名
  6. MATLAB double、str、cell间的类型转换
  7. if ($this-getRequest()-isPost()) {
  8. 三年研发、数亿美元成本,Mate 20的“大杀器”麒麟980是怎样炼成的?
  9. 替代left join方法_你应该使用pathlib替代os.path
  10. 何为计算机视觉?计算机视觉与数字图像处理的区别、Opencv的起源。
  11. 利用 Web Share API 将网页分享到 App(下)
  12. 八、IO优化(7)减少IO竞争
  13. T100服务端接口开发步骤
  14. fseek 函数(易懂)
  15. 计算机中找计算器二进制转换咋找,计算器自带二进制转换器,你值得学习
  16. 转: Github上关于iOS的各种开源项目集合
  17. 使用FFmpeg进行mp4与m3u8之间转换
  18. Luat模块应用手册-示例-Luat DEMO-长连接超低功耗方案
  19. 2015年医学核心期刊目录(有效期自2014年)
  20. iOS 视频直播技术

热门文章

  1. 公司招聘专员爆头痛哭,求职者再拒绝我的邀请我就要。。。
  2. Python吴恩达深度学习作业4 -- 构建深度神经网络
  3. 天勤数据结构代码——链表基本操作
  4. Android 多种简单的弹出框样式设置
  5. python之禅中英版
  6. Java对字符串中数字进行按自然顺序排序
  7. 【线性模型引论】王松桂著 课后习题4.3参考答案
  8. JAVA移位运算符-左移、右移、无符号右移 (简解)
  9. addEventListener和attachEvent的区别
  10. 鲍威尔法源程序码matlab,鲍威尔算法matlab程序f.doc