in reply to Advanced replace function

... or, one of several possible alternates, on the off chance (ymmv) you find this will be easier to read someday, down the road:

my $regex = qr'(?:\[.*?\])'; compile regex; use non-capture grouping $str =~ s/$regex/~~/g;

Replies are listed 'Best First'.
Re^2: Advanced replace function
by ikegami (Patriarch) on Dec 05, 2009 at 05:50 UTC

    You are advocating building small patterns so that they can be combined into larger patterns, right? If so, your code is buggy (and I don't mean just the superfluous (?:)).

    >perl -E"$sqr=qr/\[.*?\]/; $_='ab[cd]ef[gh]'; s/$sqr$//; say" ab

    Fixed:

    >perl -E"$sqr=qr/\[[^\[\]]*\]/; $_='ab[cd]ef[gh]'; s/$sqr$//; say" ab[cd]ef
Re^2: Advanced replace function
by AnomalousMonk (Archbishop) on Dec 05, 2009 at 04:34 UTC

     qr// automatically wraps its expression in a non-capturing group:

    >perl -wMstrict -le "my $regex = qr'(?:\[.*?\])'; print $regex; " (?-xism:(?:\[.*?\]))