我想找到子字串的(原始)索引,例如"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"不管插入的字符/字串等?
uj5u.com熱心網友回復:
您可以在模式中包含要“忽略”的部分:
re.search("c(<ee>)*a(<ee>)*c",string)
對你string來說,它會產生
<re.Match object; span=(3, 10), match='c<ee>ac'>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/476787.html
標籤:Python python-3.x 正则表达式 细绳
