Regular expressions, parsing data file

B

Brian Andrus

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
 
G

Gunnar Hjalmarsson

Brian said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top