08-12-2018, 04:35 AM | #1 |
Enthusiast
Posts: 36
Karma: 10
Join Date: Jul 2015
Device: Kobo H2O Edition 2
|
Need simple RegEx function help for adding tags to be found in ToC
Hi, I'm not a programmer (I only know basic HTML) and I've gone over the RegEx basic tutorial on Calibre's website - and it's helped me for years on editing garbage out of OCR documents, etc. However, a common need I have is to quickly add entries to a table of contents (especially because table of contents is a primary tool I use for many books on my e-reader). Often books will follow a format for their chapter/section headings, but they won't be tagged as headers. I can manually turn them into (for example) <h3> tags, and then go to the book editor tool and "generate ToC from all headings" or "from major headings". When a book only has a dozen or so ToC entries I want, then I just do that manually. However, I would love a more automatic way to do that with RegEx find and replace, especially when a book has a hundred or more entries I want to put in the ToC (for example, a cookbook).
So let me give an example for what I'm currently working on. There's a cookbook that has each section as a ToC entry, but each section has within it dozens or even hundreds of recipes. I want each one to be a ToC sub-entry without having to do it all manually. I found the RegEx for finding each recipe header, but I don't know how to create the "replace" portion of the text that will use the text that's already there and just add an <h3> tag around it. Here's some example code of what I want turn into ToC entries - this is how two of the different examples are already coded in the book: Code:
<p class="left-bullet"></p> <p class="left"><strong>Apple-Walnut Pancakes</strong></p> <p class="left-bullet"></p> <p class="left"><strong>Whole Wheat Orange Bread</strong></p> Code:
<p class="left-bullet">*</p> <p class="left"><strong>.*</strong></p> Code:
<p class="left-bullet">(however many bullets it found)</p> <h3>(Whatever the "find" text was for the recipe title)</h3> I appreciate any help that any of you can offer! Thanks a lot! |
08-12-2018, 06:49 AM | #2 |
Enthusiast
Posts: 36
Karma: 10
Join Date: Jul 2015
Device: Kobo H2O Edition 2
|
OK, I did more searching in the forum and read some examples that happened to show what I needed. Putting parentheses around my expressions essentially numbers what's found within them for later reference (such as in the "replace" section):
Code:
<p class="left-bullet">(*)</p> <p class="left"><strong>(.*)</strong></p> Code:
<p class="left-bullet">\1</p> <h3>\2</h3> Now that I understand that, I went back into the RegEx FAQ and the Quick reference page to see if it was explained there... and now that I understand what it means, I can see it's referred to there. However, it certainly wasn't clear and it would be clarified a lot if an example (such as mine) were given. Actually, the real example that helped me figure it out was taken from here: https://www.mobileread.com/forums/sh...=replace+regex The person's question and the other's response helped me see what was different about it, experiment a little, and figure it out. Still, I think more "grouping" explanation would help a LOT for those new to RegEx (like me) who are reading the FAQ. They should put an extra example in there, and talk about how surrounding your expression with parentheses makes a group of it and can be recalled later by choosing it's number in the order in which it was grouped (and give examples so they can see what you're talking about). Anyway, that's my suggestion. Just glad I found my answer because now I have several hundred entries quickly added to my ToC without manually doing it all! |
Advert | |
|
08-12-2018, 10:29 AM | #3 | |
Wizard
Posts: 2,115
Karma: 8796704
Join Date: Jun 2010
Device: Kobo Clara HD,Hisence Sero 7 Pro RIP, Nook STR, jetbook lite
|
Quote:
bernie |
|
08-13-2018, 10:15 AM | #4 | |
Enthusiast
Posts: 36
Karma: 10
Join Date: Jul 2015
Device: Kobo H2O Edition 2
|
Quote:
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
RegEx Function: Title Case | phossler | Editor | 29 | 07-04-2020 10:52 AM |
Random number in Regex Function? | nqk | Editor | 2 | 05-23-2017 11:47 PM |
RegEx-Function and hyphenation problem | scratch | Editor | 4 | 01-28-2017 12:44 PM |
Regex Function about «» and | senhal | Editor | 8 | 04-06-2016 02:12 AM |
Use Regex to Code an Inline TOC, from an External TOC's .ncx File | mostlynovels | ePub | 2 | 03-16-2011 12:15 PM |