in reply to Longest match finding.

Make sure your matches are tested in a specific order: longest to shortest.
if ( /567./ ) { # port 2 } elsif ( /56../ ) { # port 1 } elsif ( /5.../ ) { # port x } else { # what do we do in this case? }
There might be more elegant or compact ways to express that (e.g. using an array of regex patterns), but the basic requirement is to test the patterns in order of relative specificity.

Replies are listed 'Best First'.
Re^2: Longest match finding.
by Anonymous Monk on Dec 17, 2008 at 09:25 UTC
    But these patterns are not known while coding time. It will be extracted from live gateways.

      Well, how do you as a human know which pattern to use?

      Encode the same criterion in your program.

      Maybe you meant to direct to the "most specific" pattern. Then just sort the patterns according to their specificity:

      my @sorted_patterns = sort { my $spec_a = ($a =~ tr[.][.]); my $spec_b = ($b =~ tr[.][.]); $spec_b <=> $spec_a || $a cmp $b } @patterns;