in reply to Re: Re: Reducing a line read into $_
in thread Reducing a line read into $_

The '?' gives you non-greedy matching. It shouldn't matter on the data that we've been shown, but if there's a chance that the data will have another ']' character later in the string, then the '?' becomes essential. Compare these:

$_ = '[Remove this] and nothing else'; s/\[.*\]//g; print; $_ = '[Remove this] and [nothing] else'; s/\[.*\]//g; print;

and then try then again with the '?'.

--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re: Re: Re: Re: Reducing a line read into $_
by Daniellek (Sexton) on Jan 04, 2001 at 16:13 UTC
    Your wisdom is superb!

    So for the problem of this thread, possibly the best possible will be: s/^\[.*?\]//g;

    Am I right?

    -- Daniellek
      The best would probably be considered to be s/^\[[^\]]*\]//g which says take a [, as many non ['s as possible and a ] and get rid of them. The dot is slightly often misused in regexen and you should try not to get the habit of using it where another construct is more suitable otherwise you may be bitten later on. I suggest you read at least Death to Dot Star! and the replies to it for some more background.

        I'd be interested in hearing why you think your solution is better than ^\[.*?\]. I think that the non-greedy "?" eliminates all of the potential .* issues in this case.

        Of course, I haven't benchmarked the solutions yet.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me