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

Your wisdom is superb!

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

Am I right?

-- Daniellek

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

        I'm not sure how the benchmarks would actually be, I'm speaking more from my understanding of other people's explanations of how they work (probably wrong).
        • \[.*?\] - starting at the first [ look at up all sizes of .*? from the longest to the shortest, then for which ones are followed by a ], choose the shortest one.
        • \[[^\]]*\] - starting at the first [ search through all possible [^\]]* from shortest to longest, until we find an ], at that point choose the previous one.
        Or something like that. I think I find the second logic to make more sense to me.

        Update: and the other point is I'm suggesting avoiding the habit of overusing the dot, which can cause problems such as were addressed in the thread I linked.