Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Formats > ePub

Notices

Reply
 
Thread Tools Search this Thread
Old 06-25-2016, 12:29 AM   #1
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
Need RegEx help (if it's even at all possible to do!)

Ugh! I don't know the first thing about RegEx, and although it's definitely something I'd love to learn, right now my problem is a little more immediate -- not to mention probably rather complicated (RegEx-wise) -- and I"m really hoping someone can help me out.

I'm working on a book with a few plays of Shakespeare, and have them all nicely coded and stuff. Shakespeare is well known for writing in verse, of course, but many of the characters' lines are also in prose. In that regard, I have two ways of coding up those lines, depending on whether it's in verse or in prose.

I think you should probably be able to see what I've done here, without getting into all the CSS and everything (the only real difference has to do with what happens if/when the line wraps, whether it gets indented or not).

Here's an example of a bit of verse...

Code:
  <p class="speaker">1 Witch</p>

  <p class="verse">When shall we three meet againe?</p>

  <p class="verse">In Thunder, Lightning, or in Raine?</p>
...and here's an example of some prose...

Code:
  <p class="speaker">Hamlet</p>

  <p class="prose">Get thee to a Nunnerie. Why would’st thou be a breeder of Sinners? I am my selfe indifferent honest, but yet I could accuse me of such things, that it were better my Mother had not borne me. I am very prowd, reuengefull, Ambitious, with more offences at my becke, then I haue thoughts to put them in imagination, to giue them shape, or time to acte them in. What should such Fellowes as I do, crawling betweene Heauen and Earth. We are arrant Knaues all, beleeue none of vs. Goe thy wayes to a Nunnery. Where’s your Father?</p>
Now, what I'm trying to accomplish is for if the speaker's line is in verse, then I want to wrap a div around the speaker's name (which is on one line) and that first line of verse, with that div being a CSS class that adds in page-break:avoid.

Does that make sense, what I'm doing, and why I want to do it?

So basically, to use that first example of some verse, it would then look like this...

Code:
  <div class="pageavoid">
    <p class="speaker">1 Witch</p>

    <p class="verse">When ſhall we three meet againe?</p>
  </div>

  <p class="verse">In Thunder, Lightning, or in Raine?</p>
...with that "pageavoid" div wrapped around the "speaker" paragraph and the first "verse" paragraph.

The problem is that I've got almost 3000 of those "speaker" lines -- and each one I would have to actually look at and see if the next line is verse or prose, and either way I then have to add (or not) the "pageavoid" div.

Is there ANY way to do a RegEx search & replace that could do this for me automagically (and save me several days of utter, unbearable drudgery)? Basically, I'd hope that there's a way to search for every instance of a "speaker" paragraph, and then if the next line is a "verse" paragraph, then wrap that "pageavoid" div around them both. Otherwise, if it's a "prose" paragraph, then don't do anything.

I hope I've explained the problem okay, and what it is that I'm trying to accomplish -- and thanks SO much in advance, if this is possible!

Last edited by Psymon; 06-25-2016 at 02:32 AM.
Psymon is offline   Reply With Quote
Old 06-25-2016, 12:55 AM   #2
mattmc
Connoisseur
mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.mattmc can program the VCR without an owner's manual.
 
Posts: 81
Karma: 185923
Join Date: May 2015
Device: iPad 1/2/Air, K3/PW2/Fire1, Kobo Touch, Samsung Tab, Nook Color/Touch
Quote:
Originally Posted by Psymon View Post
Basically, I'd hope that there's a way to search for every instance of a "speaker" paragraph, and then if the next line is a "verse" paragraph, then wrap that "pageavoid" div around them both. Otherwise, if it's a "prose" paragraph, then don't do anything.
Bit of a stretch for RegEx, honestly. How's your Javascript/jQuery?
mattmc is offline   Reply With Quote
Old 06-25-2016, 02:21 AM   #3
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
Quote:
Originally Posted by mattmc View Post
Bit of a stretch for RegEx, honestly. How's your Javascript/jQuery?
About as good as my RegEx! Almost non-existent (although I do know some javascript for web pages, but nothing too extensive at all). :/
Psymon is offline   Reply With Quote
Old 06-25-2016, 02:33 AM   #4
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: 9,817
Karma: 13963768
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo: Touch, Glo, Aura H2O, Glo HD
You should be able to do this in the calibre editor. I'm not sure about Sigil or other text editors.

So, in the calibre editor...

Start by doing a tidy/reformat the code. This makes sure the layout is the same throughout the book. Put the search/replace into regex mode. Then select a sample of what needs to be changed and press CTRL-F. This takes takes the selected text and puts it into the search field. It also escapes any of the characters in the selection that need to be escaped. It also includes the line feeds that are in the selected text.

Then, change both the speakers name and the spoken text to ".*" (without the quotes) and add open and close parentheses around text.

You will end up with something like the following but on only one line in the search field:

Code:
(<p class="speaker">.*</p>

    <p class="verse">.*</p>)
The replace text is:

Code:
<div class="pageavoid">\1</div>
That should work, but I haven't tested it.
davidfor is offline   Reply With Quote
Old 06-25-2016, 02:54 AM   #5
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
Quote:
Originally Posted by davidfor View Post
That should work, but I haven't tested it.

Oh, awesome! At least, if it worked -- which it doesn't. I get a message saying "No replace function with the name: <div class="pageavoid">\1</div> exists."

I think I can sorta see what you were getting at, though, and how that might work (if it did), even without knowing RegEx at all. Perhaps I'm just missing something in there?

EDIT: I tried it just now in Sigil, too (which also has a RegEx search & replace), thinking that maybe it might work differently somehow -- and, hopefully, that this would work! -- but it didn't. No error message in there, though, it just says "No replacements made." :/

Last edited by Psymon; 06-25-2016 at 03:01 AM.
Psymon is offline   Reply With Quote
Old 06-25-2016, 03:14 AM   #6
jbacelar
Interested in the matter
jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.
 
jbacelar's Avatar
 
Posts: 186
Karma: 45000
Join Date: Dec 2011
Location: Spain, south coast
Device: PocketBook 622
Search:
Code:
\s\s<p class="speaker">(.+?)</p>\n\n\s\s<p class="verse">(.+?)</p>\n
Replace:
Code:
  <div class="pageavoid">\n\n  <p class="speaker">\1</p>\n\n  <p class="verse">\2</p>\n\n  </div>\n
jbacelar is offline   Reply With Quote
Old 06-25-2016, 03:18 AM   #7
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
Quote:
Originally Posted by jbacelar View Post
Search:
Code:
\s\s<p class="speaker">(.+?)</p>\n\n\s\s<p class="verse">(.+?)</p>\n
Replace:
Code:
  <div class="pageavoid">\n\n  <p class="speaker">\1</p>\n\n  <p class="verse">\2</p>\n\n  </div>\n

Omigod, thank you SO SO SO very much!!!

2060 changes made -- all perfectly (as far as I can see) -- in the blink of an eye! I can't tell you how incredibly grateful I am! If you're ever in Ottawa (Canada), drop me a line and I'll buy you a few beers!!!

Psymon is offline   Reply With Quote
Old 06-25-2016, 03:27 AM   #8
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
PS. @davidfor... You get a beer, too, for taking a shot at it!
Psymon is offline   Reply With Quote
Old 06-25-2016, 03:28 AM   #9
jbacelar
Interested in the matter
jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.
 
jbacelar's Avatar
 
Posts: 186
Karma: 45000
Join Date: Dec 2011
Location: Spain, south coast
Device: PocketBook 622
Quote:
Originally Posted by Psymon View Post
If you're ever in Ottawa (Canada), drop me a line and I'll buy you a few beers!!!
Sure!
jbacelar is offline   Reply With Quote
Old 06-25-2016, 04:10 AM   #10
GeoffR
Wizard
GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.
 
GeoffR's Avatar
 
Posts: 2,851
Karma: 7185821
Join Date: Nov 2012
Location: New Zealand
Device: Kobo Glo
Quote:
Originally Posted by Psymon View Post
Code:
  <div class="pageavoid">
    <p class="speaker">1 Witch</p>

    <p class="verse">When ſhall we three meet againe?</p>
  </div>

  <p class="verse">In Thunder, Lightning, or in Raine?</p>
Do you need to add a div at all? I think adding this to the CSS stylesheet should accomplish the same thing:
Code:
p.speaker + p.verse {
  page-break-before: avoid;
  page-break-inside: avoid;
}
GeoffR is offline   Reply With Quote
Old 06-25-2016, 04:24 AM   #11
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
Quote:
Originally Posted by GeoffR View Post
Do you need to add a div at all? I think adding this to the CSS stylesheet should accomplish the same thing:
Code:
p.speaker + p.verse {
  page-break-before: avoid;
  page-break-inside: avoid;
}

Oh, that's interesting -- something like that never even occurred to me. Would that wrap the page-break:avoid around both items, together, though? Or would it add it to each individually/separately? If the latter, then it wouldn't accomplish what I'm trying to, of course.
Psymon is offline   Reply With Quote
Old 06-25-2016, 05:36 AM   #12
GeoffR
Wizard
GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.GeoffR ought to be getting tired of karma fortunes by now.
 
GeoffR's Avatar
 
Posts: 2,851
Karma: 7185821
Join Date: Nov 2012
Location: New Zealand
Device: Kobo Glo
Quote:
Originally Posted by Psymon View Post
Oh, that's interesting -- something like that never even occurred to me. Would that wrap the page-break:avoid around both items, together, though? Or would it add it to each individually/separately? If the latter, then it wouldn't accomplish what I'm trying to, of course.
Actually it is not exactly the same as wrapping a div around p.speaker and p.verse:

p.speaker+p.verse selects the first p.verse following any p.speaker, so the page-break-before:avoid; prevents a break between p.speaker and p.verse, and page-break-inside:avoid; prevents a break inside the p.verse immediately following the p.speaker, but unlike with the wrapper div it is still possible to break inside the p.speaker.

So you would also need to add
Code:
p.speaker {
  page-break-inside: avoid;
}
but it is not possible to select only the p.speaker preceding a p.verse so this would have to apply to all p.speaker.
GeoffR is offline   Reply With Quote
Old 06-25-2016, 05:51 AM   #13
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
Quote:
Originally Posted by GeoffR View Post
Actually it is not exactly the same as wrapping a div around p.speaker and p.verse

Thanks for the clarification on that, Geoff!

Back to the whole RegEx thing, can anyone recommend a good tutorial "for dummies" on that subject? I'm not asking anyone to do my googling for me, because I did do that even before posting my question here to the forum, but it seemed like I just couldn't wrap my head around the tutorials I found -- it was all just too "programmy" (so to speak) for me.

This was something about myself that I discovered when I first got into web design back in the mid-1990s, too. I had no problem with HTML and stuff, and CSS as well when that eventually came around (I'm old!) but when it came to learning the more "hard-core" programming stuff like javascript, etc. I had a lot of difficulty getting it. I'm not sure why that is, but I basically just came to the conclusion that I'm just much more suited to design-oriented coding than the "programmy"-type stuff.

But it would be really handy to be able to understand RegEx, for sure! I just couldn't find any that were "easy," for semi-stupid people like myself.

Any recommendations on any tutorial sites out there?
Psymon is offline   Reply With Quote
Old 06-25-2016, 01:11 PM   #14
jbacelar
Interested in the matter
jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.jbacelar is that somebody.
 
jbacelar's Avatar
 
Posts: 186
Karma: 45000
Join Date: Dec 2011
Location: Spain, south coast
Device: PocketBook 622
Quote:
Originally Posted by Psymon View Post
Any recommendations on any tutorial sites out there?
http://www.regular-expressions.info/tutorialcnt.html

This tutorial is the best I know.
I think that to solve 90% of the problems that arise in our hobby, we need to know until Grouping chapter.

Good luck!
jbacelar is offline   Reply With Quote
Old 06-25-2016, 01:37 PM   #15
Psymon
Chief Bohemian Misfit
Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.Psymon can even cheer up an android equipped with a defective Genuine Personality Prototype.
 
Psymon's Avatar
 
Posts: 490
Karma: 30686
Join Date: May 2013
Device: iPad, ADE
Quote:
Originally Posted by jbacelar View Post
http://www.regular-expressions.info/tutorialcnt.html

This tutorial is the best I know.
I think that to solve 90% of the problems that arise in our hobby, we need to know until Grouping chapter.
Thanks! I'll give that one a serious shot -- I'd really like to learn this, because it obviously makes the world of difference for easily accomplishing things that would otherwise be a veritable nightmare to do (like the problem of mine that spurred this thread).

I don't know why I have such a hard time with "programmy"-type stuff like this. I do just fine with HTML/CSS, but it's as though something short-circuits in my brain and I become a complete moron with this kind of thing. :/

But I'll try my best to learn it -- no promises that I won't get stuck again and be back here for more help down the road, though! Forgive me... in advance... if that happens.

Thanks again to everyone for all the help!
Psymon is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex help anyone? seanos Editor 17 04-02-2014 11:03 AM
New help with a regex txckie Calibre 2 08-29-2011 08:46 PM
Help me with regex please. eVrajka Library Management 5 08-15-2011 12:17 PM
What a regex is Worldwalker Calibre 20 05-10-2010 05:51 AM
Help with a regex A.T.E. Calibre 1 04-05-2010 07:50 AM


All times are GMT -4. The time now is 05:17 AM.


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