Solved

Pattern Matching

Posted on 2002-06-14
7
177 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
ID: 7079106
$respstr =~ s/(<.*?>)/ /g;
0
 
LVL 84

Expert Comment

by:ozo
ID: 7079115
$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
ID: 7079241
mzehner,
Could you give us a sample of the html you want to substitute?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 84

Expert Comment

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

Expert Comment

by:jhurst
ID: 7081006
$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
ID: 7081331
Regexp below will also get things like
<img alt="-->" src="arrow.gif">

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

Author Comment

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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl split by | 2 101
Convert grep lines to perl 6 43
Parse csv file and generate graphs in HTML in bash 8 188
Formatting stings with pack and printf in perl 5 73
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 …
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…

831 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