Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Editor

Notices

Reply
 
Thread Tools Search this Thread
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
Reply

Tags
find, regex-function, replace


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
clear dropdown find and replace list? pompano Sigil 8 10-09-2013 06:50 AM
MS Word Find & Replace and "Smart" quotes question. Ahu Lee Workshop 1 01-14-2013 11:02 AM
Replace multiple matching instances within paragraph? murphycc Conversion 2 02-23-2012 09:53 AM
Find/Replace Won't Find Rand Brittain Sigil 7 09-24-2011 04:35 AM
Help with Word - Find & Replace Big Kev Workshop 3 09-21-2010 06:51 PM


All times are GMT -4. The time now is 08:06 PM.


MobileRead.com is a privately owned, operated and funded community.