Solved

substitution

Posted on 2000-03-28
6
219 Views
Last Modified: 2010-03-05
I have an HTML file with comments like:

<!-- cgi:SOMETHING -->

I want to parse that file under two conditions. The first condition will replace those comments with data like($buffer contains the entire HTML file):

$buffer =~ s/<!-- cgi:SOMETHING -->/$my_data/g;

The other instance of parsing this file get rid of those comments alltogether. So I want to parse $buffer and anywhere it sees a comment, it replaces it with nothing. However, the SOMETHING in the comment will be different for each comment, so how would I write the substitution command to replace all comments with nothing?

-Mike K.
0
Comment
Question by:lunaboy
6 Comments
 
LVL 2

Expert Comment

by:djsansui
ID: 2665993
I think what you're aiming for is:
$buffer =~ s/<!--(.|\n)*-->//g;
0
 
LVL 1

Author Comment

by:lunaboy
ID: 2666031
no, that deletes all the lines that contain comments... I just need to delete the comments themselves...
0
 
LVL 1

Author Comment

by:lunaboy
ID: 2666034
no, that deletes all the lines that contain comments... I just need to delete the comments themselves...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Accepted Solution

by:
PC_User321 earned 50 total points
ID: 2666067
$buffer =~ s/<!--.*?-->//g;
0
 
LVL 5

Expert Comment

by:pitonyak
ID: 2666079
Will the comments have any particular structure such as potentially including embedded HTML then you have a little problem. My concern is that you will end your replacement before you need to. Also, will there be any escape characters?

0
 
LVL 1

Author Comment

by:lunaboy
ID: 2666103
pitonyak - The comments will only be like:

<!-- cgi: SOMETHING -->

where SOMETHING is any single word. It's used for a template. The script parses the HMTL file, replaces the comments with the appropriate data, and prints it. However, I have another script that I wanted to print the same HTML without the dynamic data. So I wanted that script to replace all those comments with nothing.

PC User321 answered correctly, he will get the points this time.

-Mike K.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

808 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