在搜索保留原始索引的不同子字符串时排除常量子字符串

Exclude constant substring while searching for different substring remaining original indexing

我想找到子字符串的(原始)索引,例如"cac" 同时忽略一些其他(常量)子字符串,例如"<ee>".

import re

string = "aaac<ee>acbbb"
pattern=re.compile("<ee>") # pattern to exclude
re.search("cac", pattern.sub("",string))

我试过使用正则表达式,但这只给我新建立的字符串的索引(不包括模式):

<re.Match object; span=(3, 6), match='cac'> 

有没有办法获取 "cac" 的第一个和最后一个索引,而不考虑插入的 charcters/strings 等?

您可以在您的模式中包含要“忽略”的部分:

re.search("c(<ee>)*a(<ee>)*c",string)

对于您的 string,它产生

<re.Match object; span=(3, 10), match='c<ee>ac'>