Python 正则表达式在搜索时忽略某些字符

Python 正则表达式在搜索时忽略某些字符,python,regex,Python,Regex,我希望能够匹配字符串中的子字符串,但我希望我的搜索对原始字符串中插入的一些预定义字符具有鲁棒性。举个例子: string = "This is a text containing several sentences. This is a first test string\n\n. This test string should also be matched\t." substring = "This is a first test string. This test string shoul

我希望能够匹配字符串中的子字符串,但我希望我的搜索对原始字符串中插入的一些预定义字符具有鲁棒性。举个例子:

string = "This is a text containing several sentences. This is a first test string\n\n. This test string should also be matched\t."
substring = "This is a first test string. This test string should also be matched."
我想返回原始字符串中子字符串的索引(通常是
re.search(substring,string,re.IGNORECASE).span()


如何在搜索时忽略这些元字符(\n,\t)?

在执行
查找
之前,从
字符串中删除
\n
\t
。您不需要执行
re.search

>>> re.sub(r'[\n\t]+', '', string).lower().find(substring.lower())
45