写出正则表达式,从一个字符串中提取链接地址。比如下面字符串中
"IT面试题博客中包含很多  <a href=http://hi.baidu.com/mianshiti/blog/category/微软面试题> 微软面试题 </a> "
则需要提取的地址为 " http://hi.baidu.com/mianshiti/blog/category/微软面试题 "

在python中:
import re
p = re.compile('<a(?: [^>]*)+href=([^ >]*)(?: [^>]*)*>')
content = "IT面试题博客中包含很多 <a href=http://hi.baidu.com/mianshiti/blog/category/微软面试题> 微软面试题 </a> "
p.search(content).groups()
这段代码对于给出的例子是足够了,但实际情况中还需要考虑链接地址两边的单引号或者双引号,href的大小写,情况会稍微复杂些。
另外,如果面试者对正则表达式完全没有概念,可以和面试官申请换一道题,一般不会有太大影响。
参考资料:
http://wiki.ubuntu.org.cn/Python正则表达式操作指南

相关文章:

  • 2022-01-15
  • 2022-12-23
  • 2021-06-18
  • 2021-11-25
  • 2021-10-20
  • 2021-07-12
猜你喜欢
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
相关资源
相似解决方案