Python Forum
Matching whole words in find/replace script
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching whole words in find/replace script
#3
(Jul-10-2017, 07:10 PM)greektranslator Wrote: I use the code below through Notepad++ but I cannot make it match whole words

with open('C:/Temp/Substitutions.txt') as f:
    for l in f:
        s = l.split()
        editor.replace(s[0], s[1])
I also tried:

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

editor = re.sub(r'\b' + s[0] + r'\b', s[1], editor)
This works for me
Output:
>>> re.sub(r'\b'+'CD'+r'\b','XX','ABCD CD CDAB ABCDEF CD') 'ABCD XX CDAB ABCDEF XX'
So I would suspect that the contents of s[0] aren't what you think they are (you don't even trim() the input lines)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
RE: Matching whole words in find/replace script - by Ofnuts - Jul-10-2017, 08:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  find and group similar words with re? cartonics 4 820 Oct-27-2023, 05:36 PM
Last Post: deanhystad
  package script cant find sibling script when executed from outside Bock 3 956 Mar-03-2023, 04:26 PM
Last Post: snippsat
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,652 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Find and Replace numbers in String giddyhead 2 1,296 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Regular Expression for matching words xinyulon 1 2,219 Mar-09-2022, 10:34 PM
Last Post: snippsat
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,892 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Find and replace in files with regex and Python Melcu54 0 1,878 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  find an replace not in all entries Monsterherz 1 1,977 Mar-01-2021, 03:59 PM
Last Post: BashBedlam
  Trying to find first 2 letter word in a list of words Oldman45 7 3,847 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  Find and replace to capitalize with Regex hermobot 2 2,576 Mar-21-2020, 12:30 PM
Last Post: hermobot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020