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

Find and replace in string re.insensitive

I have this code to find matches in a string, im using in a search to mark the wordds that match my search, i need this to be case insensitive, the issue here is that it replaces the word by the one we search.

val_to_searchp = "this Text string has alot of teXt"
word = "TEXT"
    
pal2rep = str(":::")+word+str(":::")
val_to_search = re.sub(re.escape(word), pal2rep, val_to_searchp, flags=re.IGNORECASE)

this will return

"this :::TEXT::: string has alot of :::TEXT:::"

I need it to return

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

"this :::Text::: string has alot of :::teXt:::"

Also tryed with this but its not working very well 🙁

f = 0
s = 0
val_to_search = val_to_searchp
for m in re.finditer(str(word), str(val_to_searchp)):
      inicio = int(m.start()+s)
      fim = int(m.end()+f)
                
      val_to_search = val_to_search[:inicio] \
             + str(":::") \
             + val_to_search[inicio:fim] \
             + str(":::") \
             + val_to_search[fim:].strip()

      f = f+2
      s = s+1

This is my actuall code

def findtext():
    if len(str(findtext_inp.get('1.0', END)))>1:
        val_to_searchp = str(respon_txt.get(1.0, END).replace(html.unescape('⛔'), "").strip())
        respon_txt.delete(1.0, END)
        word = str(findtext_inp.get('1.0', END).strip())

        pal2rep = str(str(html.unescape('⛔'))+word+str(html.unescape('⛔')))
        val_to_search = re.sub(re.escape(word), pal2rep, val_to_searchp, flags=re.IGNORECASE)


        """
        f = 0
        s = 0
        for m in re.finditer(str(word), str(val_to_search)):
            inicio = int(m.start()+s)
            fim = int(m.end()+f)
        
            val_to_search = val_to_search[:inicio] \
                + str(html.unescape('⛔')) \
                + val_to_search[inicio:fim] \
                + str(html.unescape('⛔')) \
                + val_to_search[fim:].strip()

            f = f+2
            s = s+1
        """


    respon_txt.insert(1.0, val_to_search)#val_to_search.replace(findtext_inp.get('1.0', END).strip() , str(html.unescape('⛔')+findtext_inp.get('1.0', END).strip())+html.unescape('⛔')))

>Solution :

I’m sure there’s a way to do this with RE but it’s really trivial without the aid of that module.

val_to_searchp = "this Text string has alot of teXt"
text = 'TEXT'

def func(s, txt):
    txt = txt.lower()
    for i, e in enumerate(t := s.split()):
        if e.lower() == txt:
            t[i] = f':::{e}:::'
    return ' '.join(t)

print(func(val_to_searchp, text))

Output:

this :::Text::: string has alot of :::teXt:::
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