![]() |
#1 |
Enthusiast
![]() Posts: 27
Karma: 10
Join Date: Jul 2010
Device: sony prs-600
|
REGEX find and replace help please
I have code that looks like:
<p class="calibre1">69</p> Where 69 is a chapter number and I want them all to be like: <h3>Chapter 69</h3> obviously there are lots of <p class="calibre1"> Wild card <p class="calibre1">??< can find all the chapters but I have no way of resolving ?? to the chapter number. I understand that REGEX could do this... any chance of a clue here - It may also help me to understand the concept. Thanks in anticipation. ![]() |
![]() |
![]() |
![]() |
#2 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1281258
Join Date: Sep 2009
Device: PRS-505
|
Find:
<p class="calibre1">(\d+)</p> Replace: <h3>Chapter \1</h3> |
![]() |
![]() |
Advert | |
|
![]() |
#3 | ||
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 655
Karma: 64171
Join Date: Sep 2010
Location: Kent, England, Sol 3, ZZ9 plural Z Alpha
Device: Sony PRS-300, Kobo Aura HD, iPad (Marvin)
|
Try
serach Quote:
Quote:
Edit: Doh` Too slow |
||
![]() |
![]() |
![]() |
#4 |
Enthusiast
![]() Posts: 27
Karma: 10
Join Date: Jul 2010
Device: sony prs-600
|
Thanks both..
The worked fine. I guess the (\d+) or ([0-0]+) stores the numbers (how many? all until </p>?) an \1 recalls them (the first set I suppose)? If I had more than one they woould be \2..\3 etc.. anyway it works much thanks. anyway it worked |
![]() |
![]() |
![]() |
#5 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 655
Karma: 64171
Join Date: Sep 2010
Location: Kent, England, Sol 3, ZZ9 plural Z Alpha
Device: Sony PRS-300, Kobo Aura HD, iPad (Marvin)
|
\d means a digit
[0-9] means any character 0,1,2,3,4,5,6,7,8,9 the + means match as many as possible the brackets mean store this as a set as this was surrounded by the<p> and </p> then we mean only line that start <p> have only digits (no other characters/symbols etc.) and then finishes with </p> We replace the whole section (including the <p> and </p>) with the <h3>Chapter and then stored set (as you said), then finishes it of closing the <h3>. It's worth reading up and trying to understand, as regex's make some thing a lot easier, (such as what you wanted) but as powerful as they can be, they can cause undesired results unless you know what your doing, so when starting to use regex's save often and if needed, quit out and re-load. |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Zennist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,022
Karma: 47809468
Join Date: Jul 2010
Device: iPod Touch, Sony PRS-350, Nook HD+ & HD
|
I have a very similar question. I saw this thread so I thought I would just tack my question on to this.
If I want to replace a set of tags with another set, but the text in between the tags varies from instance to instance, with no definable pattern, and there are a hundred instances, how do I do that? For example, I want to replace ... <p class="calibre1">xyz text</p> with ... <blockquote class="calibre20">xyz text</blockquote> I tried a wildcard search and was able to locate the first expression but wasn't able to figure out how to retain the text in between the tags when the tags were replaced. Can someone help? |
![]() |
![]() |
![]() |
#7 |
Reader
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 519
Karma: 24612
Join Date: Aug 2009
Location: Utrecht, NL
Device: Kobo Aura 2, iPhone, iPad
|
Put () around the part that you want to retain, then in the replacement use \1 to get it. If there are more () the the search expression you can use \1, \2, etc, just in order of the ().
|
![]() |
![]() |
![]() |
#8 |
Zennist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,022
Karma: 47809468
Join Date: Jul 2010
Device: iPod Touch, Sony PRS-350, Nook HD+ & HD
|
pietvo, thanks! Worked well.
|
![]() |
![]() |
![]() |
#9 |
Enthusiast
![]() Posts: 27
Karma: 10
Join Date: Jul 2010
Device: sony prs-600
|
Thanks to all.
It has helped lots. I now see the light. |
![]() |
![]() |
![]() |
#10 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1281258
Join Date: Sep 2009
Device: PRS-505
|
The most important thing is always to make sure that you have 'minimal matching' checked, otherwise bad things can happen.
|
![]() |
![]() |
![]() |
#11 |
Enthusiast
![]() Posts: 27
Karma: 10
Join Date: Jul 2010
Device: sony prs-600
|
OK
I have <h3 id="heading_id_2">Chapter 1</h3> <p class="MsoNormal3"><span class="calibre62">one two three</span></p> and I want <h3 id="heading_id_2">Chapter 1 - one two tree</h3> I can get the 1 with \d but how do I pick up one two three the length and number of words here is variable I tried \w+ to no avail? Thanks in anticipation |
![]() |
![]() |
![]() |
#12 |
Enthusiast
![]() Posts: 27
Karma: 10
Join Date: Jul 2010
Device: sony prs-600
|
I got it..
FIND
<h3 id="heading_id_(\d+)">Chapter (\d)</h3> <p class="MsoNormal(\d+)"><span class="calibre(\d+)">(.+)</span></p> Replace <h3>Chapter \2 - \5</h3> works OK. after a cup of coffee Last edited by potestus; 09-18-2010 at 07:29 AM. |
![]() |
![]() |
![]() |
#13 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 655
Karma: 64171
Join Date: Sep 2010
Location: Kent, England, Sol 3, ZZ9 plural Z Alpha
Device: Sony PRS-300, Kobo Aura HD, iPad (Marvin)
|
potestus, where you have in the find string, Chapter (\d), be aware that that will only do Chapter 1-9, any double digit chapters will not be done, change it to (\d+) as you have the others to do multiple digits.
|
![]() |
![]() |
![]() |
#14 |
Enthusiast
![]() Posts: 27
Karma: 10
Join Date: Jul 2010
Device: sony prs-600
|
Thanks,
yes I got that in my moment of light. Then I opened up and found chapters up to 25. may thanks. |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help with Word - Find & Replace | Big Kev | Workshop | 3 | 09-21-2010 06:51 PM |
find and replace, epub | ampsonic | Reading and Management | 3 | 03-02-2010 03:20 AM |
Find/Replace with regular expression | hydrolith | Sigil | 6 | 03-01-2010 08:42 PM |
Crashes on find / replace all | mr ploppy | Sigil | 1 | 02-03-2010 12:35 PM |
Using Find/Replace with BD | Otter | Sony Reader | 7 | 02-28-2009 01:49 PM |