Craig Cardimon wrote:
> I need to strip HTML-style "<" and ">" tags and their contents from
> ASCII text while not disturbing customized tags that might
> say <TYPE> or
> <DESCRIPTION>. Is there a way to do this without going bonkers?
> 
> I'm using
> 
> ***
> 
> s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
> 
> ***
> 
> to strip all angle braces from the text. It works like gangbusters,
> but I've discovered it works a little too well.
> 
> Now I need something I can apply more judiciously. Any hints, tips, or
> suggestions would be appreciated.

Assuming that "HTML-style" is sufficiently different from HTML
(otherwise the answer would be HTML::Parser), and based on your regex
working "like gangbusters", you could defer the decision of what markup
to keep or discard to a subroutine, e.g.

s/(<(?:[^>'"]*|(['"]).*?\1)*>)/transform($1)/egs;

Where the subroutine looks something like (untested):

my %keepit = ("<TYPE>" => 1, "<DESCRIPTION>" => 1);
sub transform {
        if (defined $keepit{$_[0]}) {
                return $_[0];
        }
        else {
                return "";
        }
}

As an aside, your regex is looking a bit hairy. I would suggest using x
switch, possibly with the qr operator, so that you can break the regex
into multiple lines with embedded comments. I find it easier to follow
what is going on.

HTH

-- 
Brian Raven
 


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary 
companies.
-----------------------------------------------------------------------


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to