Python正则表达式:'exceptions.IndexError:字符串索引超出范围'

时间:2014-08-05 22:56:03

标签: python regex scrapy

我使用一个简单的正则表达式来替换'id =“111111”格式的字符串实例。这些字符串是作为使用scrapy生成的网页响应的一部分派生的,并使用另一个正则表达式进行过滤,只给出我想要的输出。我正在以下列方式使用re.sub:

match3 = re.sub("/id="[0-9]+"/", ' ', match3)

但是,这会引发以下错误:

exceptions.IndexError: string index out of range

任何人都可以向我解释这里的问题是什么吗?

由于

1 个答案:

答案 0 :(得分:1)

  1. python中的模式不需要分隔符。
  2. 当您使用"[0-9...
  3. 时,您正在关闭模式(这是一个字符串)

    正确的方法是:

    match3 = re.sub( 'id="\d+"', ' ', match3 )   # using a different enclosure
    

    match3 = re.sub( "id=\"[0-9]+\"", ' ', match3 )  # escaping the "