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 07-02-2020, 08:11 AM   #1
AlwaysNew
Member
AlwaysNew began at the beginning.
 
Posts: 14
Karma: 10
Join Date: Jun 2020
Device: Kindle
Question Regex replace p-class in entire book

Hi, I'm new at this so bear with me.

Trying to replace this...

Code:
 <p class="p23">[keep everything inside here]</p>
with this...

Code:
 <h1 class=“p15”>[keep everything inside here]</h1>
throughout all chapters in book.

I've tried Diaps Plugin but it's a bit beyond me. Any help sincerely appreciated!
Thanks.
AlwaysNew is offline   Reply With Quote
Old 07-02-2020, 08:38 AM   #2
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 73,896
Karma: 128597114
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
Search for...
Code:
<p class="p23">(.*)</p>
Replace with...
Code:
<h1 class="p15">\1</h1>
JSWolf is offline   Reply With Quote
Advert
Old 07-03-2020, 10:26 AM   #3
AlwaysNew
Member
AlwaysNew began at the beginning.
 
Posts: 14
Karma: 10
Join Date: Jun 2020
Device: Kindle
Thanks JS, but the replace code you suggested replaces the entire chapter.

Quote:
Originally Posted by JSWolf View Post
Replace with...
Code:
<h1 class="p15">\1</h1>
I need to replace one open and closed p tag (with a nested span and chapter title inside it) with an h1 tag.

That is, from this:
Code:
 <p class="p23"><span id="chapter-5"></span>Coming Home</p>
To this:
Code:
 <h1 class="p15"><span id="chapter-5"></span>Coming Home</p>
And I want to do that with every succeeding chapter in the book.

Thanks in advance for your help!
AlwaysNew is offline   Reply With Quote
Old 07-03-2020, 12:09 PM   #4
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 921
Karma: 1177583
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
Quote:
Originally Posted by AlwaysNew View Post
Thanks JS, but the replace code you suggested replaces the entire chapter.



I need to replace one open and closed p tag (with a nested span and chapter title inside it) with an h1 tag.
This will do:

Search:
Code:
<p class="p23">(.*?)</p>
Replace:
Code:
<h1 class="p15">\1</h1>
thiago.eec is offline   Reply With Quote
Old 07-03-2020, 02:04 PM   #5
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
Join Date: Jan 2009
Location: Valley Forge, PA, USA
Device: Kindle Paperwhite
Quote:
Originally Posted by AlwaysNew View Post
I've tried Diaps Plugin but it's a bit beyond me. Any help sincerely appreciated!
Thanks.
IMHO it's well worthwhile to learn Diaps PI -- I use it all the time and it's a lot easier that debugging some complicated RegEx

It's configurable so you can add additional tags

This does what you asked, but it you want to key off of the id="chapter.." it's a little more complicated

Result is

Code:
<h1 class="p15"><span id="chapter-5"></span>Coming Home</h1>
Attached Thumbnails
Click image for larger version

Name:	Capture.JPG
Views:	180
Size:	97.0 KB
ID:	180360  
phossler is offline   Reply With Quote
Advert
Old 07-04-2020, 11:21 AM   #6
AlwaysNew
Member
AlwaysNew began at the beginning.
 
Posts: 14
Karma: 10
Join Date: Jun 2020
Device: Kindle
Quote:
Originally Posted by thiago.eec View Post
This will do:

Search:
Code:
<p class="p23">(.*?)</p>
Replace:
Code:
<h1 class="p15">\1</h1>
Thanks Thiago!! It did the trick! Much appreciated :-)

Last edited by AlwaysNew; 07-04-2020 at 11:24 AM.
AlwaysNew is offline   Reply With Quote
Old 07-04-2020, 11:28 AM   #7
AlwaysNew
Member
AlwaysNew began at the beginning.
 
Posts: 14
Karma: 10
Join Date: Jun 2020
Device: Kindle
Quote:
Originally Posted by phossler View Post
IMHO it's well worthwhile to learn Diaps PI -- I use it all the time and it's a lot easier that debugging some complicated RegEx

It's configurable so you can add additional tags

This does what you asked, but it you want to key off of the id="chapter.." it's a little more complicated

Result is

Code:
<h1 class="p15"><span id="chapter-5"></span>Coming Home</h1>
Thanks Phossler. That screenshot is helpful. I will try get my head around it. In general, the object here is to clean up Calibre's code from an imported Pages epub so I end up with a lean mobi for Kindle :-)
AlwaysNew is offline   Reply With Quote
Old 07-05-2020, 04:10 PM   #8
gbm
Wizard
gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.
 
Posts: 2,082
Karma: 8796704
Join Date: Jun 2010
Device: Kobo Clara HD,Hisence Sero 7 Pro RIP, Nook STR, jetbook lite
Quote:
Originally Posted by AlwaysNew View Post
Thanks Phossler. That screenshot is helpful. I will try get my head around it. In general, the object here is to clean up Calibre's code from an imported Pages epub so I end up with a lean mobi for Kindle :-)
This is very useful page, I have it downloaded to my PC and the bookmark points to the local saved page.

Quick reference for regexp syntax

bernie
gbm is offline   Reply With Quote
Old 07-09-2020, 04:37 AM   #9
AlwaysNew
Member
AlwaysNew began at the beginning.
 
Posts: 14
Karma: 10
Join Date: Jun 2020
Device: Kindle
OK, I've tried using the regex as recommended, but now it's messing things up.

When I search this:
Code:
 <p class="p32">(.*?)</p>
And replace it with this:
Code:
 <h1 class="p20">(\1)</h1>
It changes this:
Code:
 <p class="p32"><span id="chapter-5"></span>Coming Home</p>
To this:
Code:
 <h1 class="p20">(<span id="chapter-5"></span>Coming Home)</h1>
That's just one chapter example, but it puts parentheses around all the chapter titles. What am I missing here??

Any help truly appreciated as regex is not easy for me.
AlwaysNew is offline   Reply With Quote
Old 07-09-2020, 04:39 AM   #10
georgemk
Addict
georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.georgemk ought to be getting tired of karma fortunes by now.
 
Posts: 234
Karma: 291844
Join Date: Oct 2019
Device: Kobo Nia
In the replace, you do not need the round brackets. That is why the changed version shows brackets. You asked it to add them.
georgemk is offline   Reply With Quote
Old 07-09-2020, 04:43 AM   #11
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
Posts: 29,779
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Replace NEVER gets the (search) Capture Paren
\1 is the value that will be inserted
When you included the LITERALS ( and ), that is what was ADDED, because that is what you told it to do.
theducks is online now   Reply With Quote
Old 07-09-2020, 09:01 AM   #12
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 73,896
Karma: 128597114
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
Question, why are we still going over this when @phossler gave the ultimate answer that works?
JSWolf is offline   Reply With Quote
Old 07-09-2020, 04:17 PM   #13
BobC
Guru
BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.BobC ought to be getting tired of karma fortunes by now.
 
Posts: 691
Karma: 3026110
Join Date: Dec 2008
Location: Lancashire, U.K.
Device: BeBook 1, BeBook Pure, Kobo Glo, (and HD),Energy Sistem EReader Pro +
Quote:
Originally Posted by phossler View Post
IMHO it's well worthwhile to learn Diaps PI -- I use it all the time and it's a lot easier that debugging some complicated RegEx

It's configurable so you can add additional tags
I appear to be missing something, How do you "configure" the plug-in. There is no option to do so and "Help" says it isn't configurable. Not only that back in the mists of time (2015) DiapDealer said he didn't want the PI to be able to swap tags in this way.

Does this involve some arcane tweaking of a file somewhere ?

I have long tried to use the plug-in to achieve the task of converting a classed "p" tag to a "h" tag and have always had to do it by regex. As I see it the only thing you can change a "p" to is a "div".

BobC
BobC is offline   Reply With Quote
Old 07-09-2020, 04:40 PM   #14
phossler
Wizard
phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.phossler ought to be getting tired of karma fortunes by now.
 
Posts: 1,071
Karma: 412718
Join Date: Jan 2009
Location: Valley Forge, PA, USA
Device: Kindle Paperwhite
I normally just use the parameters screen to add additional tags, but IIRC I did have to one time tweak (VERY carefully) the file:

Code:
C:\Users\......\AppData\Roaming\calibre\plugins\diaps_editing_toolba
g_SpanDivEdit_settings.json
Don't recall exactly why, but I THINK it was because I wanted to modify a tag that wasn't in the original distribution

Been awhile since, once I had everything set, there hasn't been any reason to change
Attached Thumbnails
Click image for larger version

Name:	Capture.JPG
Views:	149
Size:	41.9 KB
ID:	180508   Click image for larger version

Name:	Capture2.JPG
Views:	157
Size:	101.1 KB
ID:	180509   Click image for larger version

Name:	Capture3.JPG
Views:	158
Size:	44.8 KB
ID:	180512  

Last edited by phossler; 07-09-2020 at 04:50 PM.
phossler is offline   Reply With Quote
Old 07-10-2020, 03:11 AM   #15
AlwaysNew
Member
AlwaysNew began at the beginning.
 
Posts: 14
Karma: 10
Join Date: Jun 2020
Device: Kindle
Quote:
Originally Posted by JSWolf View Post
Question, why are we still going over this when @phossler gave the ultimate answer that works?
Sorry, my bad. I put (\1) instead of just \1 in the replace field. I'm still learning.
AlwaysNew is offline   Reply With Quote
Reply

Tags
regex


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex for replacing class AlwaysNew Editor 5 06-22-2020 08:55 AM
Search and replace returns entire field when no match is found wladdy Calibre 2 02-16-2014 01:51 AM
Help with regex POSIX class search bfollowell Sigil 7 05-21-2011 10:55 AM
regex replace??? schuster Conversion 14 01-29-2011 09:02 AM
need regex help search and replace schuster Calibre 4 01-10-2011 09:00 AM


All times are GMT -4. The time now is 10:17 AM.


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