in reply to I just cannot figure out the greedy regex

I'm going to assume you're using something along the lines of s/".*"//gs; to do this. It would explain why your example text would be completely removed. One way to fix this would be to use the '?' character to indicate non-greediness: s/".*?"//gs;. A better way would be to use a negated character class. An example using this follows:

#!c:/perl/bin/perl -w $|++; use strict; my $txt = <<'TXT_DONE'; This is an "example", where all of the "quotes in this text" are removed, therefore cleansing the text of "all visible quotes". "So this quote", as well as "this quote here" will be stripped. TXT_DONE $txt =~ s/"[^"]+"//g; print $txt; __END__ This is an , where all of the are removed, therefore cleansing the text of . , as well as will be stripped.

Replies are listed 'Best First'.
Re: Re: I just cannot figure out the greedy regex
by Roger (Parson) on Dec 16, 2003 at 05:33 UTC
    Just to ammend to the post by Coruscate that you might also want to handle escaped quotes with the following regex:
    s/"(?:\\"|.)*?"//g;

      And just to amend to the post by Roger that you might also want to remove quoted strings with newlines in them, lest you wish to end up with very strange results, with the following regex:

      s/"(?:\\"|.)*?"//gs;