in reply to Removing html comments with regex

Im trying to store a snippet along with other html in a file from a form textarea field. Everything else stores in there. The comment makes the stuff in the snippet dissapear. I tried removing other regex to avoid any conflicts. If I remove one of the comments, the snippet shows up even without the regex. Otherwise, the snippet dissapears
<!-- comments -->
snippet of code
<!-- comments -->

Replies are listed 'Best First'.
Re: Re: Removing html comments with regex
by esh (Pilgrim) on Aug 23, 2003 at 06:33 UTC

    You have provided a regular expression and some sample data. You say that the regular expression removes "snippet of code" from your sample data, but it does not.

    Here, I've adapted blokhead's framework and plugged in your regular expression and your data:

    #!/usr/bin/perl -w use strict; my $comments = do { local $/; <DATA> }; print "BEFORE:\n$comments"; $comments =~ s/<!--(.|\s)*?-->//g; print "AFTER:\n$comments"; __DATA__ <!-- comments --> snippet of code <!-- comments -->
    The output is:
    BEFORE: <!-- comments --> snippet of code <!-- comments --> AFTER: snippet of code
    If you want to have someone debug your problem, you'll have to provide the real regular expression you're using or the real data. (And, yes, I did test it out with multi-line comments, too.)

    BTW, $comments is a little strange as a name for text which includes more than just comments. Are you sure that it contains the snippet of code before you apply the regular expression?

    -- Eric Hammond