我使用一个简单的正则表达式来替换'id =“111111”格式的字符串实例。这些字符串是作为使用scrapy生成的网页响应的一部分派生的,并使用另一个正则表达式进行过滤,只给出我想要的输出。我正在以下列方式使用re.sub:
match3 = re.sub("/id="[0-9]+"/", ' ', match3)
但是,这会引发以下错误:
exceptions.IndexError: string index out of range
任何人都可以向我解释这里的问题是什么吗?
由于
答案 0 :(得分:1)
"[0-9...
正确的方法是:
match3 = re.sub( 'id="\d+"', ' ', match3 ) # using a different enclosure
或
match3 = re.sub( "id=\"[0-9]+\"", ' ', match3 ) # escaping the "