in reply to Reducing a line read into $_

s/\[.*?\]//;

Replies are listed 'Best First'.
Re: Re: Reducing a line read into $_
by Daniellek (Sexton) on Jan 04, 2001 at 15:18 UTC
    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

      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