Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace with "whole word only" option [duplicate]

Tags:

I have a script that runs into my text and search and replace all the sentences I write based in a database.

The script:

with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
    for l in f:
        s = l.split('*')
        editor.replace(s[0],s[1])

And the Database example:

Event*Evento*
result*resultado*

And so on...

Now what is happening is that I need the "whole word only" in that script, because I'm finding myself with problems.

For example with Result and Event, because when I replace for Resultado and Evento, and I run the script one more time in the text the script replace again the Resultado and Evento.

And the result after I run the script stays like this Resultadoado and Eventoo.

Just so you guys know.. Its not only for Event and Result, there is more then 1000+ sentences that I already set for the search and replace to work..

I don't need a simples search and replace for two words.. because I'm going to be editing the database over and over for different sentences..

like image 708
Renan Cidale Avatar asked Jul 18 '13 18:07

Renan Cidale


People also ask

How to Replace multiple words in a file?

Remove all the files you don't want to edit by selecting them and pressing DEL, then right-click the remaining files and choose Open all. Now go to Search > Replace or press CTRL+H, which will launch the Replace menu. Here you'll find an option to Replace All in All Opened Documents.

How do you replace a whole word in Python?

Use re. sub instead of normal string replace to replace only whole words.So your script,even if it runs again will not replace the already replaced words.

How do you replace multiple words with one word in Python?

Approach 1: join(), split() and list comprehension. In this approach, we will first declare a list with words that have to be replaced and then traverse to that words in the string and replace it with the letter k. The join() method is used to join list elements together.


4 Answers

You want a regular expression. You can use the token \b to match a word boundary: i.e., \bresult\b would match only the exact word "result."

import re

with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
    for l in f:
        s = l.split('*')
        editor = re.sub(r"\b%s\b" % s[0] , s[1], editor)
like image 100
kindall Avatar answered Sep 22 '22 23:09

kindall


Use re.sub:

replacements = {'the':'a', 
                'this':'that'}

def replace(match):
    return replacements[match.group(0)]

# notice that the 'this' in 'thistle' is not matched 
print re.sub('|'.join(r'\b%s\b' % re.escape(s) for s in replacements), 
        replace, 'the cat has this thistle.') 

Prints

a cat has that thistle.

Notes:

  • All the strings to be replaced are joined into a single pattern so that the string needs to be looped over just once.

  • The source strings are passed to re.escape to make avoid interpreting them as regular expressions.

  • The words are surrounded by r'\b' to make sure matches are for whole words only.

  • A replacement function is used so that any match can be replaced.

like image 45
Steven Rumbalski Avatar answered Sep 21 '22 23:09

Steven Rumbalski


Use re.sub instead of normal string replace to replace only whole words.So your script,even if it runs again will not replace the already replaced words.

>>> import re
>>> editor = "This is result of the match"
>>> new_editor = re.sub(r"\bresult\b","resultado",editor)
>>> new_editor
'This is resultado of the match'
>>> newest_editor = re.sub(r"\bresult\b","resultado",new_editor)
>>> newest_editor
'This is resultado of the match'
like image 13
DhruvPathak Avatar answered Sep 23 '22 23:09

DhruvPathak


It is very simple. use re.sub, don't use replace.

import re
replacements = {r'\bthe\b':'a', 
                r'\bthis\b':'that'}

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = re.sub(i,j,text)
    return text

replace_all("the cat has this thistle.", replacements)

It will print

a cat has that thistle.
like image 6
Sudharsan Avatar answered Sep 21 '22 23:09

Sudharsan