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 06-16-2024, 07:36 AM   #1
Bozana
Bozana
Bozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beautyBozana does all things with Zen-like beauty
 
Bozana's Avatar
 
Posts: 23
Karma: 32224
Join Date: Jan 2013
Device: PC
Thumbs down Help: Regex Find & Replace Chapter Numbers In Words To Incremental Digits?



I have a book that needs to replace all the chapter headings in h2 that are in words to digits/integers and increment them. I'm using Calibre Editor but happy to jump over to sigil too, to get this Epub sorted.

Example:

<h2 class="heading_sed1">

<p class="class_sex">Chapter</p>

<p class="class_sez">Eighty-One</p>

</h2>

To

<h2 class="class_sex">Chapter 81</h2>


So far, I have the search part correct with either versions:

(?s)(<h2[^<>]*>)(.+?</h2>)


Or

<h2 class="heading_sed1">

<p class="class_sex">Chapter</p>

<p class="class_sez">[a-zA-Z0-9]+</p>

</h2>


However, it's my regex replace that's giving me trouble:

<h2 class="class_sez">Chapter \i+</h2>


Is there a way to accomplish this, without manually renaming them all?

And this book has part 1 and part 2 and the increments has to start over again in part two, in the same book.

I'm grateful for any help.

Thank you!

Kindest regards,

Bozana

Last edited by Bozana; 06-16-2024 at 08:11 AM.
Bozana is offline   Reply With Quote
Old 06-16-2024, 10:31 AM   #2
skil-phil
Connoisseur
skil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animalsskil-phil is kind to children and small, furry animals
 
skil-phil's Avatar
 
Posts: 74
Karma: 6698
Join Date: Sep 2022
Location: South Africa
Device: kindle pw10
Hi,
I had the same problem.
What worked for me was using the 'editor chains' plugin.
Took a while to set it all up but once done one press and all is done.
Did another to covert Roman numerals as well.
HTH.
Phil
skil-phil is offline   Reply With Quote
Advert
Old 06-19-2024, 05:49 PM   #3
lomkiri
Zealot
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
Posts: 148
Karma: 1451628
Join Date: Jul 2021
Device: N/A
You may have a look on this thread or this message to see how to use a regex-function to numerate by increment

In your case, the function could be something like :
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    data['counter'] = data.get('counter', 0) + 1
    return f'<h2 class="class_sez">Chapter {data["counter"]}</h2>'
Note : Works only "replacing all"
For a correct numeration, be sure to be on the first page of the epub when starting the "replace all"

If you want to reset the counter at each "Part nn", capture the expression of the title of the part in a group (group 1 in my exemple) with an expression like :
(<h1[^<>]*>Part \d</h1>)|(?:<h2[^<>]*>.+?</h2>)
(up to you to adapt it to your epub)

and use a function like :
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    
    # Found a new part:
    if match.group(1):
        data['counter'] = 0
        return match.group(0)

    # else, found a chapter:
    data['counter'] = data.get('counter', 0) + 1
    return f'<h2 class="class_sez">Chapter {data["counter"]}</h2>'

Last edited by lomkiri; 06-20-2024 at 02:59 PM.
lomkiri is offline   Reply With Quote
Reply

Tags
chapter numbers in words, increment digits, incremental digits


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make regex to replace 2 spaces between words, with one space? crankypants Sigil 4 10-29-2015 12:51 PM
Curious problem with find/replace & regex JoeBloe Editor 1 11-07-2014 05:45 AM
Mass find and replace for chapter numbers Keeth Sigil 13 09-28-2014 07:32 PM
Regex Help: Find page number & Replace+Remove 2x Line Breaks in Sigil Contre-jour Sigil 9 02-01-2013 11:47 AM
Chapter detection when only digits - regex needed Perkin Calibre 15 09-20-2010 07:25 PM


All times are GMT -4. The time now is 05:38 AM.


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