Solved

closing html tags

Posted on 2004-04-02
4
206 Views
Last Modified: 2010-03-04
I  need a script that closes an open tag...However it must close a specific tag mutiple times.

Below I have the name tag, which needs to be closed through out the document.  Not just three times, but however many times it appears in a document.

<name>

<name>

<story>
story tag text
</story>

<name>



Should look like:


<name>
</name>
<name>
</name>
<story>
story tag text
</story>

<name>
</name>


FYI:
The script below skips the name tag if two appear in a row.  
perl -ape '/^\s*<name>/&&do{$h=1;next;};/^\s*</&&do{if ($h==1) {print "</name>\n";$h=0;}};'
0
Comment
Question by:kennybud416
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 25 total points
ID: 10746602
perl -ape '/^\s*</&&do{if ($h==1) {print "</name>\n";$h=0;}};/^\s*<name>/&&do{$h=1;next;};'
0
 
LVL 3

Assisted Solution

by:PerlKing
PerlKing earned 25 total points
ID: 10755473
You assume that there would be only one <name> tag per line.  What if something like the following occurs?

<name> <name>
<name>
<story>
story text
</story>

Does this ever happen? If it does, the following code takes care of it,
perl -ape '/^\s*</ && do { if ($h==1) {print "</name>\n";$h=0;} }; /^\s*<name>/&&do{s/<name>(.*)</<name>$1</name></g; $h=1;next;};'
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question