View Single Post
Old 12-30-2020, 02:59 PM   #1
Yadang
Junior Member
Yadang began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Dec 2020
Device: none
Find and replace first x instances of each word from a list of words

I'm trying to underline the first x instances of each word from a list of words (over 1000 words).

Using the Regex-function mode in the Editor Find/Replace I have been able to find and replace all instances of a word with:

Code:
<u>word</u>
to underline the word. However, I'd like to be able to underline only the first or second instance of each word.

This is the code I have that replaces all instances:

Find:

Code:
>[^<>]+<
Replace:

Code:
#create list of unknown words stored in a text file
import io
myfile = io.open("C:/Users/adame/Desktop/Python/unknown_words.txt", encoding="utf-8")
unknown_words = []
for line in myfile:
* * unknown_words.append(line.strip())
myfile.close()


def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
* * replacestr = ""

* * for word in unknown_words:
* * * * replacestr = (replacestr + ".replace('" + word + "', '<u>' + '" + word + "' + '</u>')")
* * * * myreplace = "match.group()" + replacestr
* * return eval(myreplace)
An aside: it seems like there should be a better way of doing this than making a string consisting of 1000s of

Code:
.replace(unknown_word, '<u>' + unknown_word + '</u>')
and then executing these... If you have a better idea please let me know!
Yadang is offline   Reply With Quote