Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Formats > ePub

Notices

Reply
 
Thread Tools Search this Thread
Old 04-01-2024, 08:47 AM   #1
darrnih
Junior Member
darrnih began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Apr 2024
Device: none
how to replace text with Search and Replace with regex on Calibre

So, I have a book I'm listening to using text-to-speech, but at the start of each chapter, the heading is written twice. With the tags "<h1>" and "<p>".
This ends up showing the title of the chapter in both heading and normal fonts, which isn't an issue when reading, but when listening, the text-to-speech will have to repeat it twice which is a bit annoying.
To solve this, I downloaded calibre and discovered Regex, so I went about a beginner tutorial and realised that I could isolate each instance this happened.
If I have something like
Quote:
Chapter 21: The New Beginning
Chapter 21: The New Beginning
Which would appear like this in the editor.
Code:
<h1>Chapter 21: The New Beginning</h1>
<p>Chapter 21: The New Beginning</p>
By running the code below in the search tab to search through the entire file
Code:
<h1.*?>Chapter .*?</h1>
    <p>Chapter.*?</p>
I'm able to capture any instance of the title appearing twice.
My issue is this, by pasting
Code:
<h1.*?>.*?</h1>
in the replace tab, instead of it replacing the text with what was supposed to be inside the "<h1>" tag, so in this case I expect to see
Quote:
Chapter 21: The New Beginning
Instead I get,
Quote:
<h1.*?>.*?</h1>
Is there a way to save the string gotten from the first code into the second
darrnih is offline   Reply With Quote
Old 04-01-2024, 04:11 PM   #2
DNSB
Bibliophagist
DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.
 
DNSB's Avatar
 
Posts: 40,599
Karma: 157444382
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
Try wrapping the .*? in brackets. i.e. (.*?) which can be referenced by number. In the preplacement box, the first instance of (.*?) would be represented by \1, the second by \2, etc.

In your case I would use <h1 class="(.*?)">(.*?)</h1> <p class=".*?">.*?</p> as the search string and <h1 class="\1">\2</h1> as the replacement string.

If the classes are fixed, you could simply use <h1 class="whatever">(.*?)</h1> and \1 for the chapter title in the replacement string.

A quick visit to Regex 101 and playing with their test environment is a good idea.

Last edited by DNSB; 04-01-2024 at 04:14 PM.
DNSB is offline   Reply With Quote
Advert
Old 04-02-2024, 03:10 AM   #3
darrnih
Junior Member
darrnih began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Apr 2024
Device: none
Thank you very much. It worked
darrnih is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex search and replace yamusing Library Management 12 05-07-2020 02:19 PM
Regex in search problems (NOT Search&Replace; the search bar) lairdb Calibre 3 03-15-2017 08:10 PM
regex search/replace - how to? Alt68er Sigil 1 03-11-2014 09:53 PM
Regex search and replace dwlamb Sigil 6 04-12-2013 03:34 PM
regex search/replace Sharlene Sigil 10 01-28-2012 05:14 AM


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


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