Search code examples
regexperlstripping

How can I strip block comments with Perl?


I am working on a preprocessor that is analyzing a DSL. My goal is to remove the comments. The block comment facility is demarcated by %% before and after. I do not have to worry about %% being in strings, by the definition of the language.

I am using this s/// regex. Unfortunately, it seems to match everything and wipe it out:

#Remove multiline comments.
$text_string =~ s/%%.*%%//msg;

What am I doing wrong?


Solution

  • the first thing you can do is make it non-greedy:

    .*?
    

    otherwise,

    %% some text %%

    real content

    %% other text %%

    will all be wiped out.