in reply to Re: Simple Regex Question
in thread Simple Regex Question

$area =~ s/^.*\s+.*\s+(.*?)-dpt/\1/;

Not very efficient. The RE engine will have to work more than you think to match that pattern. From my test of it, it will turn a line like this:

asdasd	egg	nyc-dpt	net	10
into
nyc	net	10

Did you test this before posting, or look at the other answers? :)

Cheers,
KM

Replies are listed 'Best First'.
Re: Re: Re: Simple Regex Question
by sierrathedog04 (Hermit) on Jan 30, 2001 at 02:52 UTC
    Your point is well taken. From now on I will test my answers first.

    My proposed solution is:

    use strict; my $row1 = "newjersey-ab1.net agg nj1-dpta1 net 10"; $row1 =~ s/^.+\s+.+\s+(.+)-dpt.*$/\1/; print $row1;

    As far as looking at the other answers, yes I look at them. If my approach differs from the other answers then I like to throw it out there and see what people say.

    I agree with you that my answer may be inefficient; I really only starting doing Perl seriously last year. My question to anyone who cares to answer is, why is it inefficient? And is this inefficiency lost in the noise of overall execution times, or would it be a problem in real-life?

      This is a good article explaining what happens inside the RE engine. Take what you have vs other answers and see what the RE engine would do with them. Also add the fact that the other answers looked for a match, and didn't do a substitution (which isn't needed in this case).

      And is this inefficiency lost in the noise of overall execution times

      No.. it could make the execution time longer. If you ran that match against 10_000 lines, it would produce it's own noise.. compared to the other solutions.

      I'm not trying to come down on ya :) Read that article, read perlre, and Mastering Regular Expressions. Then, practice working with REs. I bet in a few weeks you would look at this and say "I am now so much wiser!" :)

      Cheers,
      KM

        Fair enough. I will try and avoid posting my own regex solutions on this site until I have read Mastering Regular Expressions and the article you recommended.