鱼C论坛

 找回密码
 立即注册
查看: 1607|回复: 5

[已解决]【正则表达式】匹配出错。。。原谅我实在找不了不同

[复制链接]
发表于 2018-2-8 23:14:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 脑子 于 2018-2-8 23:17 编辑

对于以下俩坨:
  1. <a href="/htmlnews/2018/2/401948.shtm" target="_blank" onmouseover="javascript:this.style.color='#ba1414'" onmouseout="javascript:this.style.color='#333333'" style="color: rgb(51, 51, 51);">六年200高校更名:想成名校不能只改校名</a>

  2. <a href="/htmlnews/2018/2/402593.shtm" target="_blank">
  3.                                                   香港设立抗战博物馆
  4.                                               </a>
复制代码


我想把
  1. htmlnews/2018/2/401948.shtm" target="_blank" onmouseover="javascript:this.style.color='#ba1414'" onmouseout="javascript:this.style.color='#333333'" style="color: rgb(51, 51, 51);">六年200高校更名:想成名校不能只改校名</a>
复制代码


  1. htmlnews/2018/2/402593.shtm" target="_blank">
  2.                                                   香港设立抗战博物馆
  3.                                               </a>
复制代码

用正则表达式给找出来。

然而。。。
  1. >>> p=ur'/htmlnews/[^\n]+.shtm"[^。]*</a>'
  2. >>> urls=re.findall(p,'''cellspacing="0" width="100%">
  3.                                  
  4.                                       <tr onmouseover="javascript:this.style.backgroundColor='#f5ecec'" onmouseout="javascript:this.style.backgroundColor=''">
  5.                                           <td align="left" valign="top" width="60%" style="font-size:5px">
  6.                                              <img src="/images/t11.gif" alt="" /> <a  href='/htmlnews/2018/2/402590.shtm' target="_blank" >
  7.                                                   武警部队组织第一期新军事训练大纲集训
  8.                                               </a><a href="/htmlnews/2018/2/401948.shtm" target="_blank" onmouseover="javascript:this.style.color='#ba1414'" onmouseout="javascript:this.style.color='#333333'" style="color: rgb(51, 51, 51);">六年200高校更名:想成名校不能只改校名</a>''')
  9. >>> urls
  10. ['/htmlnews/2018/2/401948.shtm" target="_blank" onmouseover="javascript:this.style.color=\'#ba1414\'" onmouseout="javascript:this.style.color=\'#333333\'" style="color: rgb(51, 51, 51);">\xc1\xf9\xc4\xea200\xb8\xdf\xd0\xa3\xb8\xfc\xc3\xfb\xa3\xba\xcf\xeb\xb3\xc9\xc3\xfb\xd0\xa3\xb2\xbb\xc4\xdc\xd6\xbb\xb8\xc4\xd0\xa3\xc3\xfb</a>']
复制代码

如上,我的正则只能匹配其中一个
急求解!!!
最佳答案
2018-2-9 20:35:26
本帖最后由 lies_for_L 于 2018-2-9 20:57 编辑
脑子 发表于 2018-2-9 14:38
仍然不行。。。
我发现也许原因是含有换行符的缘故?


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.*?
.*?
.*?
.*?
.*?
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1. # 测试代码
  2. import re
  3. a = '''<a href="/htmlnews/2018/2/401948.shtm" target="_blank" onmouseover="javascript:this.style.color='#ba1414'" onmouseout="javascript:this.style.color='#333333'" style="color: rgb(51, 51, 51);">六年200高校更名:想成名校不能只改校名</a>

  4. <a href="/htmlnews/2018/2/402593.shtm" target="_blank">
  5.                                                   香港设立抗战博物馆
  6.                                               </a>'''
  7. print("p=r'/htmlnews/.+?.shtm".*?</a>'测试开始")
  8. p=r'/htmlnews/.+?.shtm".*?</a>'
  9. x=re.compile(p,re.DOTALL)
  10. result = x.findall(a)
  11. print('匹配结果:%d'%len(result))
  12. for i, count in zip(result,range(1,10)):
  13.     print('第%d条匹配结果: %s'%(count, i))
  14. print('###########################################################')
  15.       
  16. print("re_pattern = '<a href="/htmlnews/[\\w/]+.*?</a>'测试开始")
  17. re_pattern = '<a href="/htmlnews/[\\w/]+.*?</a>'
  18. x=re.compile(re_pattern,re.DOTALL)
  19. result = x.findall(a)
  20. print('匹配结果:%d'%len(result))
  21. for i, count in zip(result,range(1, 10)):
  22.     print('第%d条匹配结果: %s'%(count, i))
  23. print('###########################################################')

  24. print("re_pattern = '/htmlnews/[\\w/]+\\.shtm'测试开始")
  25. re_pattern = '/htmlnews/[\\w/]+\\.shtm'
  26. x=re.compile(re_pattern,re.DOTALL)
  27. result = x.findall(a)
  28. print('匹配结果:%d'%len(result))
  29. for i, count in zip(result,range(1, 10)):
  30.     print('第%d条匹配结果: %s'%(count, i))
  31. print('###########################################################')

  32. print("re_pattern = '<a href="(/htmlnews/[\\w/]+\\.shtm).*?</a>'测试开始")
  33. re_pattern = '<a href="(/htmlnews/[\\w/]+\\.shtm).*?</a>'
  34. x=re.compile(re_pattern,re.DOTALL)
  35. result = x.findall(a)
  36. print('匹配结果:%d'%len(result))
  37. for i, count in zip(result,range(1, 10)):
  38.     print('第%d条匹配结果: %s'%(count, i))
  39. print('###########################################################')



复制代码

结果
捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-2-9 09:46:49 | 显示全部楼层
既然是网页就不要直接用正则提取,正则一般是用在最后数据提取的,之前的可以用bs4等等做处理
  1. # 整个含/htmlnews/ 的a标签
  2. re_pattern = '<a href="/htmlnews/[\\w/]+.*?</a>'
  3. # 只有href
  4. re_pattern = '/htmlnews/[\\w/]+\\.shtm'
  5. re_pattern = '<a href="(/htmlnews/[\\w/]+\\.shtm).*?</a>'
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-9 14:38:20 | 显示全部楼层
lies_for_L 发表于 2018-2-9 09:46
既然是网页就不要直接用正则提取,正则一般是用在最后数据提取的,之前的可以用bs4等等做处理

仍然不行。。。
我发现也许原因是含有换行符的缘故?

于是我修改了代码
  1. >>> p=ur'/htmlnews/.+.shtm".*</a>'
  2. >>> x=re.compile(p,re.DOTALL)
  3. >>> x.findall(s)#在DOTALL模式下,符号'.'可以匹配包括换行符在内的所有字符
复制代码


仍然不行???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-9 20:35:26 | 显示全部楼层    本楼为最佳答案   
本帖最后由 lies_for_L 于 2018-2-9 20:57 编辑
脑子 发表于 2018-2-9 14:38
仍然不行。。。
我发现也许原因是含有换行符的缘故?


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.*?
.*?
.*?
.*?
.*?
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1. # 测试代码
  2. import re
  3. a = '''<a href="/htmlnews/2018/2/401948.shtm" target="_blank" onmouseover="javascript:this.style.color='#ba1414'" onmouseout="javascript:this.style.color='#333333'" style="color: rgb(51, 51, 51);">六年200高校更名:想成名校不能只改校名</a>

  4. <a href="/htmlnews/2018/2/402593.shtm" target="_blank">
  5.                                                   香港设立抗战博物馆
  6.                                               </a>'''
  7. print("p=r'/htmlnews/.+?.shtm".*?</a>'测试开始")
  8. p=r'/htmlnews/.+?.shtm".*?</a>'
  9. x=re.compile(p,re.DOTALL)
  10. result = x.findall(a)
  11. print('匹配结果:%d'%len(result))
  12. for i, count in zip(result,range(1,10)):
  13.     print('第%d条匹配结果: %s'%(count, i))
  14. print('###########################################################')
  15.       
  16. print("re_pattern = '<a href="/htmlnews/[\\w/]+.*?</a>'测试开始")
  17. re_pattern = '<a href="/htmlnews/[\\w/]+.*?</a>'
  18. x=re.compile(re_pattern,re.DOTALL)
  19. result = x.findall(a)
  20. print('匹配结果:%d'%len(result))
  21. for i, count in zip(result,range(1, 10)):
  22.     print('第%d条匹配结果: %s'%(count, i))
  23. print('###########################################################')

  24. print("re_pattern = '/htmlnews/[\\w/]+\\.shtm'测试开始")
  25. re_pattern = '/htmlnews/[\\w/]+\\.shtm'
  26. x=re.compile(re_pattern,re.DOTALL)
  27. result = x.findall(a)
  28. print('匹配结果:%d'%len(result))
  29. for i, count in zip(result,range(1, 10)):
  30.     print('第%d条匹配结果: %s'%(count, i))
  31. print('###########################################################')

  32. print("re_pattern = '<a href="(/htmlnews/[\\w/]+\\.shtm).*?</a>'测试开始")
  33. re_pattern = '<a href="(/htmlnews/[\\w/]+\\.shtm).*?</a>'
  34. x=re.compile(re_pattern,re.DOTALL)
  35. result = x.findall(a)
  36. print('匹配结果:%d'%len(result))
  37. for i, count in zip(result,range(1, 10)):
  38.     print('第%d条匹配结果: %s'%(count, i))
  39. print('###########################################################')



复制代码

结果
捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-16 22:05:11 | 显示全部楼层
小甲鱼曾经说,面临一个问题,如果你要用正则表达式来解决,那么现在你就有两个问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-17 01:57:15 | 显示全部楼层
bit7maker 发表于 2018-2-16 22:05
小甲鱼曾经说,面临一个问题,如果你要用正则表达式来解决,那么现在你就有两个问题了

感觉到了正则的恶意
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-7 14:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表