atnonis has asked for the wisdom of the Perl Monks concerning the following question:

hello monks!
I was trying to create a pattern which for ex. holds gedit from gedit-2.2.2-i386-1
but it was a failure. can someone help me!? PS. it might not always be gedit-2.2.2-i386-1, consider the following:
gedit-2.2.2-i386-1, mod_ssl-2.8.12_1.3.27-i386-2, gnome-applets-2.2.0-i386-1 and more

atnonis!

Replies are listed 'Best First'.
Re: regex problem!
by Abigail-II (Bishop) on Jul 18, 2003 at 09:44 UTC
    Oh, great. Definition by example. And we get one whopping complete example, the other examples just show strings, but not what needs to be matched.

    Please consider defining what you want. Usually, once you give a good definition instead of a single example, it's easy to write a regex. By just giving a example, you run the risk you get 10 replies, all different. All will match your example, and all will fail on the other 1000 matches you need to perform. Some examples of regex that will match your example:

    /^(.{5})/ /(gedit)/ /(\w+)/ /([^-]+)/ /(.\w{3}.)/

    That's the danger of describing your problem with only a single example.

    Abigail

Re: regex problem!
by choocroot (Friar) on Jul 18, 2003 at 09:59 UTC
Re: regex problem!
by antirice (Priest) on Jul 18, 2003 at 09:51 UTC

    Boy oh boy...

    $name = (split/-\d/,$string)[0];

    Just never use it on anything that starts with -\d.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Question:
      Could it be that
      ($name) = split /-\d/,$string,2;
      is more efficient regarding time and/or memory than your solution?

      Just wonder because I thought that this will just split into two array elements, thus saving some memory (maybe) but at least some time.

        Yes, it is.

        Benchmark: running mine, yours for at least 3 CPU seconds... mine: 3 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 27 +8704.16/s (n=844822) yours: 3 wallclock secs ( 3.13 usr + 0.00 sys = 3.13 CPU) @ 34 +5612.45/s (n=1082739) Rate mine yours mine 278704/s -- -19% yours 345612/s 24% --

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1