鱼C论坛

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

[已解决]怎么用正则表达式要提取括号里面的东西

[复制链接]
发表于 2017-4-25 00:21:30 | 显示全部楼层 |阅读模式

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

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

x
  1. <span class="current-comment-page"[1234]</span>
复制代码

   
  1. <span class="current-comment-page"([1234])</span>
复制代码

  1. <span class="current-comment-page"[(1234)]</span>
复制代码



如题   怎么提取括号里的1234
编程小白 希望能贴上详细的代码  


谢谢
最佳答案
2017-4-25 13:04:52
一个正则就可以搞定这3个
  1. import re
  2. a = '<span class="current-comment-page"[1234]</span>'
  3. b = '<span class="current-comment-page"([1234])</span>'
  4. c = '<span class="current-comment-page"[(1234)]</span>'

  5. for i in [a,b,c]:
  6.   f = re.findall('<span class="current-comment-page".*?(\d+).*?</span>',i)[0]
  7.   print(f)

  8. >>>1234
  9. >>>1234
  10. >>>1234
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-4-25 09:28:29 | 显示全部楼层
  1. a = r'<span class="current-comment-page"[1234]</span>'
  2. p = re.compile('<span class="current-comment-page"\[(\d*)\]</span>')
  3. re.findall(p,a)
  4. ['1234']
复制代码

  1. a = '<span class="current-comment-page"([1234])</span>'
  2. p = re.compile('<span class="current-comment-page"\(\[(\d*)\]\)</span>')
  3. re.findall(p,a)
  4. ['1234']
复制代码

  1. a = '<span class="current-comment-page"[(1234)]</span>'
  2. p = re.compile('<span class="current-comment-page"\[\((\d*)\)\]</span>')
  3. re.findall(p,a)
  4. ['1234']
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 13:04:52 | 显示全部楼层    本楼为最佳答案   
一个正则就可以搞定这3个
  1. import re
  2. a = '<span class="current-comment-page"[1234]</span>'
  3. b = '<span class="current-comment-page"([1234])</span>'
  4. c = '<span class="current-comment-page"[(1234)]</span>'

  5. for i in [a,b,c]:
  6.   f = re.findall('<span class="current-comment-page".*?(\d+).*?</span>',i)[0]
  7.   print(f)

  8. >>>1234
  9. >>>1234
  10. >>>1234
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-4-25 14:54:42 | 显示全部楼层
gopythoner 发表于 2017-4-25 13:04
一个正则就可以搞定这3个

谢谢你     学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 14:57:59 | 显示全部楼层

这个输出返回的还是带括号呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 15:16:47 | 显示全部楼层
见鲸 发表于 2017-4-25 14:57
这个输出返回的还是带括号呀

。。。
re出来的当然是个列表啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-27 05:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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