Personal preference is when regexen get this large to either A) build them in steps, or B) use something else.. here is a basic filter using basic logic and tests...
#!/usr/bin/perl push(@foo, '1 60 1.2.3.4 -> 4.3.2.1 TCP 3456 > 80 [SYN]', '2 60 1.2.3.4 -> 4.3.2.1 TCP 3456 > 113 [SYN]', '3 60 1.2.3.4 -> 4.3.2.1 TCP 3456 > 123 [SYN]', '4 120 2.3.4.5 -> 5.4.3.2 ICMP ? > ? echo (ping) reply', '5 120 2.3.4.5 -> 5.4.3.2 ICMP ? > ? echo (ping) request', '6 120 2.3.4.5 -> 5.4.3.2 ICMP ? > ? echo (ping) reply', '7 60 1.2.3.4 -> 4.3.2.1 TCP 3456 > 562 [RST]', '8 60 1.2.3.4 -> 4.3.2.1 TCP 3456 > 36 [RST]', '9 60 1.2.3.4 -> 4.3.2.1 TCP 3456 > 90 [RST]', ); # assume that when split the fields are as follows.. #line = '0'; #size = '1'; #src = '2'; #dest = '4'; #proto = '5'; #port = '8'; # here is what we will test on.. this could be altered to be # collected via flags, shifted off of ARGV, or passed as # params to a CGI easily... print "proto: "; chomp(my $i_proto=<>); print "port: "; chomp(my $i_port=<>); # loop over our data set, this could just as easily be a # socket or filehandle.. for ( @foo ) { my @line = split(/\s+/); if ($i_proto) { (my $tmp = $i_proto) =~ s/^!//; if ($i_proto =~ /^!/) { next if ($line[5] =~ /$tmp/); } else { next if ($line[5] !~ /$tmp/); } } if ($i_port) { (my $tmp = $i_port) =~ s/^!//; if ($i_port =~ /^!/) { next if ($line[8] =~ /$tmp/); } else { next if ($line[8] !~ /$tmp/); } } print "$_\n"; }
I usually place a sample data line or 2 in my source file, so that people who come along after me know what elements im working on, or they can compare the data being passed to the code, vs the data the code is assuming it is receiving and go "duh.. we upgraded app X, need to alter the filter.."

I know the question was how to get a regex to match, but personally in this situation, I think it might be better to move away from the regex, as it makes the code clearer and easier to maintain..

almost update:
I guess you could also alter the split to only return the items you will ever search on, but I tend to attempt to not dictate what possible uses the code may have in the future.. A slightly better split might be something like
# @data now contains src_addr, dest_addr, proto, and port @data = ( split(/\s+/) )[2,4,5,8] # later test elem 3 instead of 5.. yada yada


MMMMM... Chocolaty Perl Goodness.....

In reply to Re: Runtime Regexp Generation by l2kashe
in thread Runtime Regexp Generation by tekkie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.