03-03-2010, 12:21 PM | #1 |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
More fun with regex / wildcards
I'm trying to automate a find and replace routine to change chapter numbers from paragraphs to headings. in other words, currently, they look like this : <p>1</p> and i want them to look like this : <h3>1</h3>
i first tried to do this with wildcards and i have no troubles finding the numbers, but unfortunately if i put <h3>?</h3> in the "replace" box, it replaces the number with "?". not helpful. so now i'm trying regex search. i looked on the regex page here to find the code : http://doc.trolltech.com/4.6/qregexp.html#introduction i should say, i know nothing about regex, but i'm trying to learn a bit. this expression <p>[0-9]{1,3}</p> finds what i want, but when i replace, i have the same problem as with the wildcards... can someone tell me, what is the regex (or wildcard !) code for "don't touch the stuff in the middle, change only the tags" ??? there must be a way to do this ! |
03-03-2010, 12:24 PM | #2 |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
oh, also, if anyone has a good (simple) tutorial for regex, i am interested !
|
Advert | |
|
03-03-2010, 12:29 PM | #3 |
eBook Enthusiast
Posts: 85,544
Karma: 93383043
Join Date: Nov 2006
Location: UK
Device: Kindle Oasis 2, iPad Pro 10.5", iPhone 6
|
In most regex implementations, the "matches" for expressions are represented in the "replace" string by "\1", "\2", etc, so to replace:
<p>nnn<p> by <h3>nnn</h3> you'd use a search string of: <p>[0-9]*</p> and a replace string of: <h3>\1</h3> where the "\1" will contain whatever was matched by the "[0-9]*". |
03-03-2010, 12:31 PM | #4 | |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
Quote:
|
|
03-03-2010, 12:38 PM | #5 |
eBook Enthusiast
Posts: 85,544
Karma: 93383043
Join Date: Nov 2006
Location: UK
Device: Kindle Oasis 2, iPad Pro 10.5", iPhone 6
|
Try using a search string of:
<p>([0-9]*)</p> and a replace string of: <h3>\1</h3> ie, with parentheses around the "search string" for the numbers. |
Advert | |
|
03-03-2010, 12:41 PM | #6 | |
Created Sigil, FlightCrew
Posts: 1,982
Karma: 350515
Join Date: Feb 2008
Device: Kobo Clara HD
|
Quote:
So like this: Code:
<p>([0-9]*)</p> Code:
<h3>\1</h3> Code:
<p>(\d+)</p> Code:
<h3>\1</h3> |
|
03-03-2010, 12:43 PM | #7 |
eBook Enthusiast
Posts: 85,544
Karma: 93383043
Join Date: Nov 2006
Location: UK
Device: Kindle Oasis 2, iPad Pro 10.5", iPhone 6
|
|
03-03-2010, 12:44 PM | #8 |
Created Sigil, FlightCrew
Posts: 1,982
Karma: 350515
Join Date: Feb 2008
Device: Kobo Clara HD
|
A good regex info site is http://www.regular-expressions.info/, and a great regex test site is Regexr. But do note that Regexr use the Perl notation for backreferences ("$#", e.g "$1", "$2"...) while Sigil uses the sed notation ("\#", e.g. "\1", "\2" ).
|
03-03-2010, 12:45 PM | #9 |
Created Sigil, FlightCrew
Posts: 1,982
Karma: 350515
Join Date: Feb 2008
Device: Kobo Clara HD
|
|
03-03-2010, 12:46 PM | #10 |
eBook Enthusiast
Posts: 85,544
Karma: 93383043
Join Date: Nov 2006
Location: UK
Device: Kindle Oasis 2, iPad Pro 10.5", iPhone 6
|
No, I realised after I'd written it that I'd missed out the parentheses. A "senior moment" on my part.
|
03-03-2010, 12:48 PM | #11 | ||
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
Quote:
Quote:
|
||
03-05-2010, 04:34 AM | #12 |
Wizzard
Posts: 1,402
Karma: 2000000
Join Date: Nov 2007
Location: UK
Device: iPad 2, iPhone 6s, Kindle Voyage & Kindle PaperWhite
|
If you're running Windows, the Regex Coach (donationware) can be a useful utility to have around.
|
03-06-2010, 09:59 AM | #13 | |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
Quote:
|
|
03-18-2010, 10:13 PM | #14 |
Wizard
Posts: 1,196
Karma: 1281258
Join Date: Sep 2009
Device: PRS-505
|
Be sure to keep the 'minimal matching' box checked whenever you use regexes (unless you're sure you don't want it). Sigil's support for non-greedy matching is very admirable and makes all the difference.
If you're experimenting with more complex regexes it's always a good idea to address the problem in stages, and save the book under a new name after each replace operation so you can backtrack if something goes wrong. |
03-28-2010, 03:51 PM | #15 |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
thanks charleski, that is very sound advice, especially as i have already gotten myself into trouble doing a "replace all" with the wrong replacement... (and that was *before* i started to tackle regex searches).
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
What a regex is | Worldwalker | Calibre | 20 | 05-10-2010 05:51 AM |
using wildcards for conversion ? | Riiyachan | Calibre | 7 | 04-20-2010 10:16 PM |
Help with a regex | A.T.E. | Calibre | 1 | 04-05-2010 07:50 AM |
wildcards in sigil | bobcdy | Sigil | 2 | 12-18-2009 10:19 PM |
Regex help... | Bobthebass | Workshop | 6 | 04-26-2009 03:54 PM |