Menu

match whole words in Python script

Help
translatum
2017-07-10
2017-07-10
  • translatum

    translatum - 2017-07-10

    I am trying to use the script below to find/replace a list of words, but I cannot manage to make it match whole words. Could you help?

    with open('C:/fix.txt') as f:
        for l in f:
            s = l.split()
            editor.replace(s[0], s[1])
    
     
  • Sasumner

    Sasumner - 2017-07-11

    You could change to a regular-expression replacemement, and add a word-boundary assertion before and after your search term:

    editor.rereplace(r'\b' + s[0] + r'\b', s[1])

     
  • translatum

    translatum - 2017-07-12

    Thank you very much!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.