Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式:';exceptions.Indexer错误:字符串索引超出范围';_Python_Regex_Scrapy - Fatal编程技术网

Python正则表达式:';exceptions.Indexer错误:字符串索引超出范围';

Python正则表达式:';exceptions.Indexer错误:字符串索引超出范围';,python,regex,scrapy,Python,Regex,Scrapy,我使用一个简单的正则表达式替换格式为'id=“111111”的字符串实例。这些字符串是作为使用scrapy生成的网页响应的一部分派生的,并使用另一个正则表达式进行过滤,以仅提供所需的输出。我正在以以下方式使用re.sub: match3 = re.sub("/id="[0-9]+"/", ' ', match3) 但是,这会引发以下错误: exceptions.IndexError: string index out of range 谁能给我解释一下这里的问题是什么 谢谢 python中的

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

match3 = re.sub("/id="[0-9]+"/", ' ', match3)
但是,这会引发以下错误:

exceptions.IndexError: string index out of range
谁能给我解释一下这里的问题是什么

谢谢

  • python中的模式不需要分隔符
  • 当您使用
    “[0-9…
  • 正确的方法是:

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

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