Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Sigil

Notices

Reply
 
Thread Tools Search this Thread
Old 11-30-2011, 06:22 PM   #1
ElMiko
Addict
ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.
 
ElMiko's Avatar
 
Posts: 320
Karma: 56788
Join Date: Jun 2011
Device: Kindle
Replacing code without replacing text?

So I've now run into this problem twice. Hopefully, the reg-ex wizards among you will have a head-slappingly simple solution to my problem.

First Case
Every new paragraph is formatted as:
Code:
<p class="calibre2"><span class="none">A Bunch of Text</span></p>
I want to change the "p class" to "calibre5" for Chapter headings only, which are currently formatted like this (1., 2., 3., ..., 21., 22., etc):
Code:
<p class="calibre2"><span class="none">1.</span></p>
Finding all the instances with reg-ex is easy enough by searching for:
Code:
<p class="calibre2"><span class="none">[0-9]+\.</span></p>
But that's as far as I can get. Replacing the above with:
Code:
<p class="calibre5"><span class="none">[0-9]+\.</span></p>
changes the number to "[0-9]+\."!

Second Case
In another book, there are random paragraph breaks mid-sentence.
My solution to that would be to find all the new paragraphs that begin with a lower-case letter, and replace all the code between it and the previous paragraph. Again, finding the instances via reg-ex proved pretty intuitive:
Code:
</p>[\s]+<p class="calibre2">[a-z]
But again, i just can't figure out how to alter the code (in this case replacing it with a blank space) without altering the first letter of the first word in that paragraph.

Looking over what I've written, I am humbled by how utterly inarticulate this all sounds, but if someone understands my gibberish, I'd be most grateful for any light you could shine on my problem.

Last edited by ElMiko; 11-30-2011 at 06:40 PM.
ElMiko is offline   Reply With Quote
Old 11-30-2011, 06:46 PM   #2
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,792
Karma: 54830978
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 ElMiko View Post
So I've now run into this problem twice. Hopefully, the reg-ex wizards among you will have a head-slappingly simple solution to my problem.

First Case
Every new paragraph is formatted as:
Code:
<p class="calibre2"><span class="none">A Bunch of Text</span></p>
I want to change the "p class" to "calibre5" for Chapter headings only, which are currently formatted like this (1., 2., 3., ..., 21., 22., etc):
Code:
<p class="calibre2"><span class="none">1.</span></p>
Finding all the instances with reg-ex is easy enough by search for:
Code:
<p class="calibre2"><span class="none">([0-9]+\.)</span></p>
But that's as far as I can get. Replacing the above with:
Add the Red
Quote:
Code:
<p class="calibre5"><span class="none">\1</span></p>
Now make the replace look like the above:
The red \1 is the found value inside the ( ) above

changes the number to "[0-9]+\."!

Second Case
In another book, there are random paragraph breaks mid-sentence.
My solution to that would be to find all the new paragraphs that begin with a lower-case letter, and replace all the code between it and the previous paragraph. Again, finding the instances via reg-ex proved pretty intuitive:
Code:
</p>[\s]+<p class="calibre2">([a-z])
But again, i just can't figure out how to alter the code (in this case replacing it with a blank space) without altering the first letter of the first word in that paragraph.
[/QUOTE]
the replace is: (space)\1
Quote:
Looking over what I've written, I am humbled by how utterly inarticulate this all sounds, but if someone understands my gibberish, I'd be most grateful for any light you could shine on my problem.
theducks is offline   Reply With Quote
Advert
Old 11-30-2011, 07:00 PM   #3
ElMiko
Addict
ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.
 
ElMiko's Avatar
 
Posts: 320
Karma: 56788
Join Date: Jun 2011
Device: Kindle
@theducks - Life. Saver. If I could impose on you for just one clarification which might help me understand the nature of my mistakes (and your solutions) better, i'd be even more grateful:

What was the significance of the parentheses? That is, how are they acting upon the expression in a way that would be distinct without them? Is it entirely so that the \1 knows what to refer to?
ElMiko is offline   Reply With Quote
Old 11-30-2011, 07:44 PM   #4
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,792
Karma: 54830978
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 ElMiko View Post
@theducks - Life. Saver. If I could impose on you for just one clarification which might help me understand the nature of my mistakes (and your solutions) better, i'd be even more grateful:

What was the significance of the parentheses? That is, how are they acting upon the expression in a way that would be distinct without them? Is it entirely so that the \1 knows what to refer to?
forward reference
\1 is the first
\2 is the second and so on
example:
FN LN:
(.+) (.+)
want LN, FN
\2, \1
theducks is offline   Reply With Quote
Old 11-30-2011, 07:50 PM   #5
sellew
Enthusiast
sellew has a complete set of Star Wars action figures.sellew has a complete set of Star Wars action figures.sellew has a complete set of Star Wars action figures.sellew has a complete set of Star Wars action figures.
 
Posts: 30
Karma: 300
Join Date: Oct 2011
Location: Barcelona
Device: Sony PRS-650, PRS-T2
I'm not very much experienced in regex but maybe I can throw some light. The parentheses are used to create a group, so that when replacing you can refer to it. That's the meaning of '\1': keep the original set of characters (the group) as it is.
You can use more than one group in the expression you want to match, for instance:
<p class="calibre2"><span class="none">([0-9]+\.)</span>blablabla([0-9])</p>
Then, when replacing, you can refer to each group using \1 and \2.
Of course you can use as many groups as required depending on the complexity of the search.
sellew is offline   Reply With Quote
Advert
Old 11-30-2011, 07:53 PM   #6
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,792
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Here is the Link to Manichean's tutorial that finally got REGEX through my thick skull

https://www.mobileread.com/forums/sho...REGEX+Tutorial
theducks is offline   Reply With Quote
Old 11-30-2011, 08:14 PM   #7
ElMiko
Addict
ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.ElMiko actually enjoys Vogon poetry.
 
ElMiko's Avatar
 
Posts: 320
Karma: 56788
Join Date: Jun 2011
Device: Kindle
@sellew - That's exactly the explanation I was looking for. Thank you for putting it so clearly! Manichean's tutorial is indeed incredibly helpful, but sometimes (oftentimes... most times...) I get lost anyway.

@ theducks - Thanks for the additional example. And sorry for being so obtuse sometimes (oftentimes... most times...)!
ElMiko is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing Books on T1 democrite Sony Reader 0 10-29-2011 06:24 PM
Replacing my Hanlin V3 maddz Which one should I buy? 0 11-25-2010 04:14 AM
Replacing my new Kobo - again! objectman Kobo Reader 7 09-20-2010 08:00 PM
Replacing the battery AprilHare Sony Reader 12 04-29-2009 01:08 PM
Replacing ¬ PieOPah Workshop 5 12-17-2008 04:25 PM


All times are GMT -4. The time now is 09:11 AM.


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