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-29-2016, 04:28 PM   #1
beic
Member
beic began at the beginning.
 
Posts: 19
Karma: 10
Join Date: May 2016
Location: España
Device: Oasis 3G
Arrow Some clarification with RegEx

Let's say I wanted to replace the string 'Sh' with the letter 'F' throughout a book:

Code:
<p class="chaptitle">The Shop</p>

<p class="indent">This should show the hashes.</p>
End result

Code:
<p class="chaptitle">The Fop</p>

<p class="indent">This fould fow the hafes.</p>

In order for it not to mess up any code, I assume I need to isolate it to within the <p> tags?

Code:
<p class="[\d\D]*?">[\d\D]*?</p>
This above matches all paragraphs regardless of class. Is this correct so far?

How would I then match all instances of 'Sh' within the <p> tags and what would the replacement text expression look like?

Thanks
beic is offline   Reply With Quote
Old 07-29-2016, 05:58 PM   #2
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
See here where I recently discussed this: https://www.mobileread.com/forums/sho...18#post3360318
eschwartz is offline   Reply With Quote
Advert
Old 07-29-2016, 06:47 PM   #3
beic
Member
beic began at the beginning.
 
Posts: 19
Karma: 10
Join Date: May 2016
Location: España
Device: Oasis 3G
Quote:
Originally Posted by eschwartz View Post
See here where I recently discussed this: https://www.mobileread.com/forums/sho...18#post3360318
Code:
(?<=>[^<]*)s\B(?=[^>]*<)
Thanks, I think that's what I'm looking for but when I paste it into the RegEx builder, the box goes red as if it's invalid. I also tried DreamWeaver's find and replace and it says RegEx syntax Error: Invalid Quantifier. This site returns the following error: "Lookbehinds need to be zero-width, thus quantifiers are not allowed".
beic is offline   Reply With Quote
Old 07-31-2016, 05:41 PM   #4
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Well, not all regex engines support variable-length lookbehinds. But I know for a fact that calibre's Editor does, because it was calibre that I tested it with!

Furthermore, calibre does not have a "RegEx builder", it has a single input box and it doesn't go red when there is an error -- it pops up an error dialog.
So clearly you did not try this regex using calibre's Editor, and given that this is the subforum for the calibre Editor, I cannot be expected to read your mind and know which piece of software you decided to use instead (without telling me).

So... try using calibre's Editor.
eschwartz is offline   Reply With Quote
Old 07-31-2016, 06:45 PM   #5
beic
Member
beic began at the beginning.
 
Posts: 19
Karma: 10
Join Date: May 2016
Location: España
Device: Oasis 3G
Thank you! I was trying to run the regex in the conversion Search & Replace tool, not the book editor

In the end I used a Regex-Function because I had a lot of characters to replace.

Past this into the find box so it will only replace text within HTML tags

Code:
>[^<>]+<
Then run a simple replace Regex-Function

Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group().replace('CH', 'Ċ').replace('GH', 'Ġ').replace('BH', 'Ḃ').replace('DH', 'Ḋ').replace('FH', 'Ḟ').replace('MH', 'Ṁ').replace('PH', 'Ṗ').replace('SH', 'Ṡ').replace('TH', 'Ṫ').replace('Ch', 'Ċ').replace('Gh', 'Ġ').replace('Bh', 'Ḃ').replace('Dh', 'Ḋ').replace('Fh', 'Ḟ').replace('Mh', 'Ṁ').replace('Ph', 'Ṗ').replace('Sh', 'Ṡ').replace('Th', 'Ṫ').replace('ch', 'ċ').replace('gh', 'ġ').replace('bh', 'ḃ').replace('dh', 'ḋ').replace('fh', 'ḟ').replace('mh', 'ṁ').replace('ph', 'ṗ').replace('sh', 'ṡ').replace('th', 'ṫ').replace('&', '⁊')
Hopefully this helps someone else.
beic is offline   Reply With Quote
Advert
Old 07-31-2016, 07:02 PM   #6
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Well, that might do it.

I have never actually used the S&R tool, because it makes more sense to use the Editor (or another Editor, before calibre had one). But for all I know it uses the builtin python stdlib re() instead of Matthew Barnett's enhanced regex module.
Anyway, I cannot guarantee how something might work if I don't use it and don't know that that is what the other guy is using.

...

Feel free to post your regex function to the relevant sticky in this subforum, if you feel other people might find it useful and want to make it more likely they will find it.
eschwartz is offline   Reply With Quote
Old 07-31-2016, 09:57 PM   #7
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,860
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
The conversion S&R uses the python re module. The regex module did not exist when it was written
kovidgoyal is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calibre 9.1 changelog clarification ElMiko Calibre 1 10-05-2012 12:25 PM
Kindle and formats clarification maciek_j Amazon Kindle 15 11-06-2010 07:54 AM
Classic Clarification? desertgrandma Barnes & Noble NOOK 4 01-11-2010 01:51 PM
Hi Everyone - Need some clarification and direction s7whitecoral Which one should I buy? 11 11-21-2009 08:20 AM
Clarification, please. desertgrandma News 4 08-24-2008 12:18 PM


All times are GMT -4. The time now is 04:39 PM.


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