Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exlude constant substring while searching for different substring remaining original indexing

I’d like to find (original) index of substring e.g. "cac" while ignoring some other (constant) substring e.g. "<ee>".

import re

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

I’ve tried using regex, but this gives me only the index of newly established string (pattern excluding):

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

Is there any way to get first and last index of "cac" regardless to inserted charcters/strings etc.?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can include the part to "ignore" in your pattern:

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

which, for your string, produces

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading