Regular expressions, parsing data file

Discussion in 'Perl Misc' started by Brian Andrus, Aug 11, 2003.

  1. Brian Andrus

    Brian Andrus Guest

    Ok, no surprise, but I have having trouble figuring out regular
    expressions.

    I want to parse a data file in perl to find a mac address of a
    particular IP.
    The format of the data file is something like:

    lease 192.168.1.199 {
    starts 1 2003/8/11 00:00:67;
    ends 1 2003/08/11 00:05:67;
    hardware ethernet 00:c0:f7:43:c0:25;
    client-hostname "brad";
    }

    So I only want to return the line hardware ethernet IF it is enclosed
    within the lease 192.168.1.199 set. Also that set shows up multiple
    times in the data file, so I want the first occurence, which isn't so
    bad. What is getting me is how do I return the first occurence of a
    line AFTER the first occurence of something else?

    I imagine it may be easier to just right a subroutine to turn on a
    flag, but I was hoping there was a one-liner type thing to do, so I
    could learn something new.

    Brian Andrus
     
    Brian Andrus, Aug 11, 2003
    #1
    1. Advertising

  2. Brian Andrus wrote:
    > The format of the data file is something like:
    >
    > lease 192.168.1.199 {
    > starts 1 2003/8/11 00:00:67;
    > ends 1 2003/08/11 00:05:67;
    > hardware ethernet 00:c0:f7:43:c0:25;
    > client-hostname "brad";
    > }
    >
    > So I only want to return the line hardware ethernet IF it is
    > enclosed within the lease 192.168.1.199 set. Also that set shows up
    > multiple times in the data file, so I want the first occurence,
    > which isn't so bad. What is getting me is how do I return the first
    > occurence of a line AFTER the first occurence of something else?


    One way: By making it non-greedy:

    ($return) = /lease 192\.168\.1\.199.+?(hardware[\w :]+);/s;
    -----------------------------------------^-------------------^

    Please read about greediness at perlre:
    http://www.perldoc.com/perl5.8.0/pod/perlre.html#Regular-Expressions

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 11, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    619
    Jay Douglas
    Aug 15, 2003
  2. Replies:
    12
    Views:
    2,086
    jan V
    Sep 15, 2005
  3. Captain Dondo

    Parsing HTML with Regular Expressions

    Captain Dondo, Jun 15, 2005, in forum: HTML
    Replies:
    7
    Views:
    644
    Gunnar Hjalmarsson
    Jun 15, 2005
  4. kevin  cline
    Replies:
    1
    Views:
    317
    Kai Schwebke
    Apr 25, 2007
  5. Noman Shapiro
    Replies:
    0
    Views:
    240
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page