• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Pattern Matching

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
mzehner
Asked:
mzehner
1 Solution
 
ozoCommented:
$respstr =~ s/(<.*?>)/ /g;
0
 
ozoCommented:
$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
 
rj2Commented:
mzehner,
Could you give us a sample of the html you want to substitute?
0
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!

 
ozoCommented:
Sorry, I meant to say
$respstr =~ s/(<.*?>)/ /gs;
0
 
jhurstCommented:
$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
 
rj2Commented:
Regexp below will also get things like
<img alt="-->" src="arrow.gif">

$respstr =~ s/<("[^"]*"|'[^']*'|[^"'>])*>/ /g;
0
 
mzehnerAuthor Commented:
Thanks!  All the answers worked quite well.  The one I selected seemed to work the best.  Thanks for the help.
0

Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now