07-29-2016, 04:28 PM | #1 |
Member
Posts: 19
Karma: 10
Join Date: May 2016
Location: España
Device: Oasis 3G
|
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> 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> How would I then match all instances of 'Sh' within the <p> tags and what would the replacement text expression look like? Thanks |
07-29-2016, 05:58 PM | #2 |
Ex-Helpdesk Junkie
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
|
Advert | |
|
07-29-2016, 06:47 PM | #3 | |
Member
Posts: 19
Karma: 10
Join Date: May 2016
Location: España
Device: Oasis 3G
|
Quote:
Code:
(?<=>[^<]*)s\B(?=[^>]*<) |
|
07-31-2016, 05:41 PM | #4 |
Ex-Helpdesk Junkie
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. |
07-31-2016, 06:45 PM | #5 |
Member
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:
>[^<>]+< 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('&', '⁊') |
Advert | |
|
07-31-2016, 07:02 PM | #6 |
Ex-Helpdesk Junkie
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. |
07-31-2016, 09:57 PM | #7 |
creator of calibre
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
|
|
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 |