In my endless search for the perfect regex, I have encountered this problem over and over again.
### Haxor ### #!/usr/bin/perl -w use strict; use warnings; my $flag = 0; my $hacklist = "hack.txt"; my $date = 'Date: '; my $octet = '(\d{1,3}\.){3}\d{1,3}'; open HACK, $hacklist or die "Can't Open $hacklist"; my @hack=<HACK>; close(HACK); foreach (@hack) { if (s/$date//){ print; $flag = 0; } if (/$octet/ and !$flag){ print STDOUT $_; print "\n"; $flag = 1; } }
Simple, parse the dates and octets from the hacklist. Currently I get this:
Thursday, December 13, 2001 07:53 AM Subject: FW: Please disconnect 208.202.183.42 thanks. Monday, December 17, 2001 08:56 AM Subject: FW: Please disconnect 213.14.199.133 thanks.
But how do I print only the octet? Everything else on that line is unecessary. Is there a way to print only the matched pattern, disregarding everything else on the line? Ideal results would be:
Thursday, December 13, 2001 07:53 AM 208.202.183.42 Monday, December 17, 2001 08:56 AM 213.14.199.133
Hopefully I will get 'Mastering Regular Expressions' for Christmas. Happy Holidays!

In reply to Octet Regex by amearse

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.