Replace all instances of an HTML tag within another

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
bzz bzz
Posts: 6
Joined: Fri Jul 04, 2008 2:28 pm

Replace all instances of an HTML tag within another

Post 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
gan

Post 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.
bzz bzz
Posts: 6
Joined: Fri Jul 04, 2008 2:28 pm

Post 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.
gan

Post 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>
bzz bzz
Posts: 6
Joined: Fri Jul 04, 2008 2:28 pm

Post 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>
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post 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
-----------------------------------------
Hope this was helpful.............good luck,
Bob
bzz bzz
Posts: 6
Joined: Fri Jul 04, 2008 2:28 pm

Post 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
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post 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?
bzz bzz
Posts: 6
Joined: Fri Jul 04, 2008 2:28 pm

Post 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!
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post 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
Hope this was helpful.............good luck,
Bob
bzz bzz
Posts: 6
Joined: Fri Jul 04, 2008 2:28 pm

Post 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
Post Reply