in reply to Need a cool RegExpresseion. plz help

I think that some work is being done without it needing to be done...
$R =~ s/\[[^\]]*?#[^\]]*?#.*?\]//gm;
Will simply remove any blocks of text enclosed in brackets, which have at least two sharp/pound signs chars in them.

Your solution is bit complicated. For example the group finding part is quantified with ?, which means match once or zero times... Also the trapping of the first group and the third one, and then rewriting them in place is a bit expensive.
The regexp seems correct other than the redundancy, as nothing is forced.

Update: Corrections based on sauoq's reply. The last one doesn't need to be negated tho, as nongreedy will match the first occurance of ]. Someone slap me next time i answer stuff at 2 am...

-nuffin
zz zZ Z Z #!perl

Replies are listed 'Best First'.
Re: Re: Need a cool RegExpresseion. plz help
by sauoq (Abbot) on Nov 20, 2002 at 00:38 UTC

    Two nits. You can write your character classes as [^]] rather than [^\]]. I think it's easier to read that way but someone else might not. More importantly, your /m modifier to the regex isn't doing a bit of good. That only affects the way anchors (^ and $) work. Perhaps you meant to use /s so that your final .* would match a newline.

    Other than that, your reply is better than mine as it actually does what he asked for rather than matching the "winning" group, which is what I did...

    Update: Just to be clear, I'd write it like s/\[[^]]*?#[^]]*?#[^]]*?\]//g. I prefer to be explicit about matching anything other than a right bracket rather than using constructs like .*?\] which work but don't really say what they mean. Avoiding them also allows you to avoid the /s modifier.

    -sauoq
    "My two cents aren't worth a dime.";