11-30-2011, 07:22 PM | #1 |
Addict
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> Code:
<p class="calibre2"><span class="none">1.</span></p> Code:
<p class="calibre2"><span class="none">[0-9]+\.</span></p> Code:
<p class="calibre5"><span class="none">[0-9]+\.</span></p> 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] 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 07:40 PM. |
11-30-2011, 07:46 PM | #2 | |||
Well trained by Cats
Posts: 30,442
Karma: 58055868
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Quote:
Quote:
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]) [/QUOTE] the replace is: (space)\1 Quote:
|
|||
Advert | |
|
11-30-2011, 08:00 PM | #3 |
Addict
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? |
11-30-2011, 08:44 PM | #4 | |
Well trained by Cats
Posts: 30,442
Karma: 58055868
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Quote:
\1 is the first \2 is the second and so on example: FN LN: (.+) (.+) want LN, FN \2, \1 |
|
11-30-2011, 08:50 PM | #5 |
Enthusiast
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. |
Advert | |
|
11-30-2011, 08:53 PM | #6 |
Well trained by Cats
Posts: 30,442
Karma: 58055868
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 |
11-30-2011, 09:14 PM | #7 |
Addict
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...)! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Replacing Books on T1 | democrite | Sony Reader | 0 | 10-29-2011 07:24 PM |
Replacing my Hanlin V3 | maddz | Which one should I buy? | 0 | 11-25-2010 05:14 AM |
Replacing my new Kobo - again! | objectman | Kobo Reader | 7 | 09-20-2010 09:00 PM |
Replacing the battery | AprilHare | Sony Reader | 12 | 04-29-2009 02:08 PM |
Replacing ¬ | PieOPah | Workshop | 5 | 12-17-2008 05:25 PM |