问一个正则表达式问题

你只是看起来很努力 2017-06-05 09:26:28
str='<img src="http://www.baidu.com/jpg.jpg" alt="hehe">'
import re
In [30]: re.findall(r'<img src="(.*?)"',str)
Out[30]: ['http://www.baidu.com/jpg.jpg']





我不理解的是 匹配出来不是应该是 <img src="http://www.baidu.com/jpg.jpg 这个样子吗?

为只配置出了括号里面的内容?
...全文
125 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
宋哥 2017-06-07
  • 打赏
  • 举报
回复
>>> m=re.match(r'<img src="(.*?)" *',s) >>> print m.group(1) http://www.baidu.com/jpg.jpg
corfox_liu 2017-06-06
  • 打赏
  • 举报
回复
括号里面的是正则的组,re.findall会返回匹配组的内容列表,参考文档是这样写的"If one or more groups are present in the pattern, retrun a list of groups" 可以这样匹配

''.join(re.findall(r'<img src=".*?"', str))
sanGuo_uu 2017-06-06
  • 打赏
  • 举报
回复
同意1# findall返回的是个list,并且是返回小括号里的东西。 你去对比下search 和 findall
shiter 2017-06-05
  • 打赏
  • 举报
回复
咦,这样的话还能把网页里面的图片地址都提取出来?
secsilm 2017-06-05
  • 打赏
  • 举报
回复
这样写就是寻找小括号里面的内容

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧