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 09-30-2015, 02:16 AM   #1
ni_c
Enthusiast
ni_c began at the beginning.
 
Posts: 40
Karma: 10
Join Date: Jul 2015
Device: none
search & replace regular expression help

Hi, im looking for a regular expression to replace html rules without changing the content inside

so if a documents sets italic rules like

<span class="italic">example one</span>
<span class="italic">example two</span>
<span class="italic">example three</span>

am looking to change them to

<i>example one</i>
<i>example two</i>
<i>example three</i>

and only know a regex search parameter of

<span class="italic">(.*?)</span>

will find all instances, but dknow the replace equivalent to keep the content

so assuming (.*?) is a search only parameter, whats the replace equivalent?

Last edited by ni_c; 09-30-2015 at 02:28 AM.
ni_c is offline   Reply With Quote
Old 09-30-2015, 02:29 AM   #2
jbacelar
Interested in the matter
jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.
 
jbacelar's Avatar
 
Posts: 421
Karma: 426094
Join Date: Dec 2011
Location: Spain, south coast
Device: Pocketbook InkPad 3
<i>\1</i>

And, I recommend: http://www.regular-expressions.info/tutorial.html

Last edited by jbacelar; 09-30-2015 at 02:33 AM.
jbacelar is offline   Reply With Quote
Advert
Old 09-30-2015, 02:33 AM   #3
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,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
The parentheses form a capture group. You can reuse a capture group in a replacement, with an escaped number -- e.g. "\1" is the replacement for the first capture group.


This is more of a general regex question. You may want to bookmark http://regular-expressions.info (my favorite regex tutorial and reference) for quick reference instead of opening a thread here
eschwartz is offline   Reply With Quote
Old 09-30-2015, 02:36 AM   #4
mikapanja
Perfectionist
mikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentametermikapanja can solve quadratic equations while standing on his or her head reciting poetry in iambic pentameter
 
Posts: 72
Karma: 12802
Join Date: Apr 2014
Device: none
Use <i>\1</i> for replace.
If covering multiple instances, go on with \2, \3 etc.
Don't forget to turn the REGEX mode on.
mikapanja is offline   Reply With Quote
Old 09-30-2015, 02:49 AM   #5
ni_c
Enthusiast
ni_c began at the beginning.
 
Posts: 40
Karma: 10
Join Date: Jul 2015
Device: none
thanks guys, had been confused by \1, last time i tried it, thought it just replaced everything with \1 for some reason. Works now, cheers, and thanks for the links, will read up on this
ni_c is offline   Reply With Quote
Advert
Old 09-30-2015, 03:24 AM   #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,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Hah, we all piled on at once
eschwartz is offline   Reply With Quote
Old 09-30-2015, 03:57 AM   #7
davidfor
Grand Sorcerer
davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.
 
Posts: 24,905
Karma: 47303824
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
Alternatively, you can use Diap's Editing Toolbag plugin. One of its functions is will do exactly this type of replacement.
davidfor is offline   Reply With Quote
Old 09-30-2015, 07:58 AM   #8
Tex2002ans
Wizard
Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.Tex2002ans ought to be getting tired of karma fortunes by now.
 
Posts: 2,306
Karma: 13057279
Join Date: Jul 2012
Device: Kobo Forma, Nook
Quote:
Originally Posted by davidfor View Post
Alternatively, you can use Diap's Editing Toolbag plugin. One of its functions is will do exactly this type of replacement.
I second this. Depending on how clean the input is, this would be more robust than a Regex solution.

Here is an image showing you the specific settings you would use when using Diap's Editing Toolbag to go from "<i>Example</i>" to "<span class="italic">Example</span>":

Click image for larger version

Name:	DiapItalics.png
Views:	198
Size:	5.5 KB
ID:	142464
Tex2002ans is offline   Reply With Quote
Old 09-30-2015, 09:42 AM   #9
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: 31,047
Karma: 60358908
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Quote:
Originally Posted by ni_c View Post
thanks guys, had been confused by \1, last time i tried it, thought it just replaced everything with \1 for some reason. Works now, cheers, and thanks for the links, will read up on this
If it DID that,
you forgot to enclose the keep this part in a positional # matching ( )
You see the \3, then the were only 2 ( ) in the search where you replace was
\1 foo \2 bar \3
theducks is offline   Reply With Quote
Old 09-30-2015, 10:07 AM   #10
ni_c
Enthusiast
ni_c began at the beginning.
 
Posts: 40
Karma: 10
Join Date: Jul 2015
Device: none
good stuff, will install that, see how it goes,

fwiw, the regex expression worked beautifully for what i actually wanted, converting destination footnote links into dual direction ones

first gave footnotes ids like id="f-1", id="f-20" etc

then converted destination placeholders like
<a id="filepos789849"></a>20.

using regex search expression
"></a>(.*?)

and replace expression
" href="Chapter-01.html#f-\1">\1</a>

resulting in
<a id="filepos789849" href="Chapter-01.html#f-20.">20.</a>

only needed to hit "replace and find" till the end of each chapter, change chapter numbers and continue, very quick, then just used normal mode to find ."> and replace with "> (to correct the ids)

Last edited by ni_c; 09-30-2015 at 10:09 AM.
ni_c is offline   Reply With Quote
Old 09-30-2015, 11:00 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: 31,047
Karma: 60358908
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Quote:
Originally Posted by ni_c View Post
good stuff, will install that, see how it goes,

fwiw, the regex expression worked beautifully for what i actually wanted, converting destination footnote links into dual direction ones

first gave footnotes ids like id="f-1", id="f-20" etc

then converted destination placeholders like
<a id="filepos789849"></a>20.

using regex search expression
"></a>(.*?)

and replace expression
" href="Chapter-01.html#f-\1">\1</a>

resulting in
<a id="filepos789849" href="Chapter-01.html#f-20.">20.</a>

only needed to hit "replace and find" till the end of each chapter, change chapter numbers and continue, very quick, then just used normal mode to find ."> and replace with "> (to correct the ids)
I would pick a safer capture pattern to avoid strays

(\d+)

Only a Integer Number (any amount digits)
theducks is offline   Reply With Quote
Old 09-30-2015, 12:31 PM   #12
Thom*
The Fumbler
Thom* began at the beginning.
 
Posts: 66
Karma: 10
Join Date: Jun 2015
Device: android 4.2/fbreader
Just search and replace

This is so simple you don't need a plugin to accomplish the task.
The only thing I would change, in order to avoid getting messed up with nested <span> tags, is to change the search string.

Search: <span class="italic">(((?!<span).)*?)</span>
Replace: <i>\1</i>

Consider nested span tags:
<span class="italic">Merry <span class="bold">Christmas </span>Everyone.</span>

Search: <span class="italic">(.*?)</span>
Replace: <i>\1</i>

Would result in:
<i>Merry <span class="bold">Christmas </i>Everyone.</span>


The search/replace that I described does a look-ahead and would not select the span because it has a nested span. You would have to address the "span class="bold" first, then rerun the "span class="italic" to modify it.
Thom* is offline   Reply With Quote
Old 09-30-2015, 12:41 PM   #13
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,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Worth noting -- much as I am a fan and advocate of lookarounds, the plugin saves you from having to do that.


For many people it's worth it for the convenience.
eschwartz is offline   Reply With Quote
Old 09-30-2015, 09:31 PM   #14
ni_c
Enthusiast
ni_c began at the beginning.
 
Posts: 40
Karma: 10
Join Date: Jul 2015
Device: none
Quote:
Originally Posted by theducks View Post
I would pick a safer capture pattern to avoid strays

(\d+)

Only a Integer Number (any amount digits)
thanks theducks, that one works perfectly
ni_c is offline   Reply With Quote
Old 10-01-2015, 10:35 AM   #15
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,087
Karma: 447222
Join Date: Jan 2009
Location: Valley Forge, PA, USA
Device: Kindle Paperwhite
Quote:
Originally Posted by ni_c View Post
thanks guys, had been confused by \1, last time i tried it, thought it just replaced everything with \1 for some reason. Works now, cheers, and thanks for the links, will read up on this
Sometimes I get confused and use the tag-ending / instead of the group replacement \

That DOES make a mess
phossler is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace question (regular expression) Funslinger Library Management 2 06-21-2013 02:12 AM
Help with regular expression search/replace bfollowell Sigil 12 06-20-2013 07:36 PM
How to obtain a new page for every chapter in regular expression (search/replace)? Ravenik Conversion 2 05-24-2012 07:04 AM
Search & Replace - Regular expression oldbwl Calibre 2 01-09-2011 09:33 AM
Find/Replace with regular expression hydrolith Sigil 6 03-01-2010 08:42 PM


All times are GMT -4. The time now is 08:47 AM.


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