Page 1 of 1
Replace all instances of an HTML tag within another
Posted: Fri Jul 04, 2008 2:39 pm
by bzz bzz
Hello all, v. impressed with your expertise in these forums. I wonder if you can help?
(Meaning, "this may be obvious to some of you, be gentle with me")
I'm looking to replace some stray HTML bold tagging that's shown up in an XML archive. So I need to remove all instances of <b> or </b>, BUT only if it occurs within the title element <atl></atl>. I need to preserve the tagging if it occurs in other elements.
Suspect that TextPad can't "find all" within an expression, and that WildEdit may have something to offer.
Also I got stuck trying to use the alternative expression (pipe).
Any helpful feedback appreciated. Thanks
Posted: Fri Jul 04, 2008 3:02 pm
by gan
If it's all on a single line or the number of lines between <atl> and </atl> is always the same it's easy, but if the <atl></atl> span several lines which might differ it might be hard using regex.....in that case you at least have to run search&replace several times. So let me know if the number of lines is always the same or not/if it's always on the same line and i can give you some examples.
Posted: Fri Jul 04, 2008 3:54 pm
by bzz bzz
In this and similar cases, the element in which I'm restricting the expression IS likely to be on the same line. - I'm not expecting <atl>Title of \n the publication</atl>, for example.
Posted: Fri Jul 04, 2008 11:55 pm
by gan
Maybe it's a bit harder then i first thought to make it perfect:) But i'm sure it should be possible to find a working solution. The best way to do it depend how the lines look like.
Is there some lines that contain several "<atl>somthing</atl>" on the same line? Like this:
Code: Select all
<atl>some <b>text</b></atl><atl>some <b>more</b> text</atl>
Or is there only a single <atl></atl> on each line?
Is there some lines where there might be many <b></b> inside a single <atl></atl> like this example:
Code: Select all
<atl>this <b>is</b> some <b>test</b></atl>
Posted: Mon Jul 07, 2008 9:04 am
by bzz bzz
Hi,
The second example is true: there is one instance of <atl></atl> in each document and there may be zero or more instances of bold markup within:
Code: Select all
<atl>this <b>is</b> some <b>test</b></atl>
Posted: Mon Jul 07, 2008 2:03 pm
by Bob Hansen
This works for no bold tags or one set of bold tags:
Search for: <atl>(.*)<b>(.*)</b>(.*)</atl>
Replace with: <atl>\1\2\3</atl>
It will also work for multiple sets of bold tags, but you need to do multiple passes.
Did a test on this:
<atl>this is some test</atl>
<atl>this <b>is</b> some test</atl>
<atl>this is some <b>test</b></atl>
<atl>this <b>is</b> some <b>test</b></atl>
Result was this, after two passes:
<atl>this is some test</atl>
<atl>this is some test</atl>
<atl>this is some test</atl>
<atl>this is some test</atl>
Use the following settings:
-----------------------------------------
[X] Regular expression
Replace All
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------
Posted: Mon Jul 07, 2008 4:37 pm
by bzz bzz
Thanks for this, much appreciated.
Tinkering with the expression, I was able to run a FIND along these lines:
Code: Select all
<atl>(.*)[<b>[^^]*</b>]*(.*)</atl>
And this is about the limit of my expertise - I can't see a way to number these instances of bold, then replace with \1\2\3 etc.
If the RegExp alone can't strip out these tags within a known string, I can at least identify the files where it occurs, and go from there.
Cheers everyone
Posted: Mon Jul 07, 2008 10:10 pm
by ben_josephs
That regular expression doesn't make a great deal of sense. Are you trying to handle all occurrences of <b> elements within an <atl> element in one go? You can't do that in TextPad. Does Bob's solution not work for you?
Posted: Tue Jul 08, 2008 8:28 am
by bzz bzz
Bob's solution works - I was in fact trying to do it all at once. If this is beyond TextPad, thanks for clarifying!
Thanks everyone for your help!
Posted: Wed Jul 09, 2008 5:01 am
by Bob Hansen
Well you could do it all at once by making a macro of the code that works, and set the macro to run multiple times.
Typical structure of the macro:
CTL-Home
Search for .......
Replace with ......
Replace all
Close Search/Replace window
Posted: Wed Jul 09, 2008 9:33 am
by bzz bzz
Thanks Bob, this was the key part that I missed - I can re-run the macro if the target string is within the <atl></atl> boundaries.
Job done. Greatly appreciated - thanks. -Rich