http://qs321.pair.com?node_id=811144


in reply to Advanced replace function

You may be looking for the /g option to s///:

my $str="abc t[poop1] def [doop2] [woop3] xyz"; $str =~ s/\[.*?\]/~~/g; print "$str\n"; __END__ abc t~~ def ~~ ~~ xyz

Replies are listed 'Best First'.
Re^2: Advanced replace function
by ikegami (Patriarch) on Dec 04, 2009 at 19:49 UTC
    There was nothing about newlines being disallowed in the brackets. The following is faster and more likely to match correctly:
    s/\[.*?\]/~~/sg

    But that's risky, especially if you use it a larger pattern. The /.*?/ could match a "[" or a "]". That's obviously undesirable. The following pattern avoids that problem:

    s/\[[^\[\]]*\]/~~/g;