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
#1
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)
Reply
#2
What exactly do you want to achieve? Matching a whole word or replace one with another.
with open('C:/Temp/Substitutions.txt') as f:
    for line in f:
        if word in line.lower(): # lower is an option here if you want case insensitive matching 
            # do something
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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
#4
On the C:/Temp/Substitutions.txt file I have a list of words separated by space. I want to match a whole word on the left, and replace it with the whole word on the right of the list. For example, if in my substitutions file there is

comp [[comp]]

I do not want it to match words like

computer
uncompare

but only instances of the word "comp" (the word may be preceded and followed by punctuation of course).
Reply
#5
I found it

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  find and group similar words with re? cartonics 4 733 Oct-27-2023, 05:36 PM
Last Post: deanhystad
  package script cant find sibling script when executed from outside Bock 3 875 Mar-03-2023, 04:26 PM
Last Post: snippsat
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,551 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Find and Replace numbers in String giddyhead 2 1,237 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Regular Expression for matching words xinyulon 1 2,176 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,807 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Find and replace in files with regex and Python Melcu54 0 1,849 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  find an replace not in all entries Monsterherz 1 1,945 Mar-01-2021, 03:59 PM
Last Post: BashBedlam
  Trying to find first 2 letter word in a list of words Oldman45 7 3,737 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  Find and replace to capitalize with Regex hermobot 2 2,520 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