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

One fast question from acolyte: why's there "?" in your regexp?
I wrote s/^\[.*\]//; and it works, so what gives us this question mark? or this is no difference?
-- Daniellek

Replies are listed 'Best First'.
Re: Re: Re: Reducing a line read into $_
by davorg (Chancellor) on Jan 04, 2001 at 15:24 UTC

    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

      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.