鱼C论坛

 找回密码
 立即注册
查看: 2899|回复: 4

关于re.sub的问题

[复制链接]
发表于 2016-4-22 22:56:57 | 显示全部楼层 |阅读模式

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

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

x
  1. import re

  2. url = 'http://bbs.fishC.com/?pageNum=2'
  3. page = 20
  4. for i in range(2, page+1):
  5.     new_link = re.sub("pageNum=\d+",'pageNum=%d' %i,re.S)
  6.     print new_link
复制代码


要打印  http://bbs.fishC.com/?pageNum=2-20 的语句,为什么会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-23 08:40:33 | 显示全部楼层
Help on function sub in module re:

sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the match object and must return
    a replacement string to be used.
第一个参数是正则,第二个是要替换的字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-23 09:22:52 | 显示全部楼层
挥舞乾坤 发表于 2016-4-23 08:40
Help on function sub in module re:

sub(pattern, repl, string, count=0, flags=0)

对呀, 第一个“pageNum=\d+" 是正则
第二个 ‘pageNum=%d' %i    是表达式 哪里错了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-23 10:14:49 | 显示全部楼层
re.sub,得到一个替换之后的新字符串,可以理解成再第3个参数中,查找第1个参数并替换成第2个,你自己看了并且确定你写的没问题吗
  1. re.sub("pageNum=\d+",'pageNum=%d' %i,re.S)
复制代码

在re.s 中匹配pageNum=\d+并替换成pageNum=%d' %i,这是你想要的吗

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
林斌 + 5 + 5 + 3 感谢!

查看全部评分

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

使用道具 举报

 楼主| 发表于 2016-4-23 10:22:23 | 显示全部楼层
挥舞乾坤 发表于 2016-4-23 10:14
re.sub,得到一个替换之后的新字符串,可以理解成再第3个参数中,查找第1个参数并替换成第2个,你自己看了并且 ...

啊啊啊。理解错了,我以为第三个参数是他的形式


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-20 19:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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