Solved

Pattern Matching

Posted on 2002-06-14
7
160 Views
Last Modified: 2012-05-04
I am writing a Perl script that should strip HTML tags from a large string and substitute a space for them.  Currently I am using the following command to do this:

$respstr =~ s/(<.*>)/ /g;

This strips anything between < and >.  Unfortunately, this strips most of the HTML since the entire page is enclosed between < and >.  I need to strip anything between < and > exclusive of content contain < or >.  How can I modify the above code to do this?

Thanks for your help.
0
Comment
Question by:mzehner
7 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
$respstr =~ s/(<.*?>)/ /g;
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
$respstr =~ s/(<.*?>)/ /gi;
But this also fails if you have HTML like:
<IMG SRC = "foo.gif" ALT = "A > B">
<!-- <A comment> -->
<script>if (a<b && a>c)</script>
0
 
LVL 10

Expert Comment

by:rj2
Comment Utility
mzehner,
Could you give us a sample of the html you want to substitute?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
Sorry, I meant to say
$respstr =~ s/(<.*?>)/ /gs;
0
 
LVL 8

Expert Comment

by:jhurst
Comment Utility
$respstr=~ s/<[^>]+>//g;

Will get all the normal tags.  You may need to do comments first if there is a risk of nested comments or worse comment round tags.
0
 
LVL 10

Accepted Solution

by:
rj2 earned 50 total points
Comment Utility
Regexp below will also get things like
<img alt="-->" src="arrow.gif">

$respstr =~ s/<("[^"]*"|'[^']*'|[^"'>])*>/ /g;
0
 
LVL 2

Author Comment

by:mzehner
Comment Utility
Thanks!  All the answers worked quite well.  The one I selected seemed to work the best.  Thanks for the help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now