Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex for DHCP leases file is not working as expected

I am using a regex pattern to match the dhcp leases data file.

(?mis)^lease\s+(\S+)(?:(?!^lease\s).)*?binding state\s+(active|inactive|offline|free|abandoned?).*?(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2});.*?vendor-class\s*(\"[^\"]+\");.*?[^}]

But the above regex is not matching properly. For example if I have a example dhcp.leases file in below format.

 lease 000.000.00.001 {
  starts 1 2022/04/25 17:31:27;
  ends 1 2022/04/25 17:33:27;
  cltt 1 2022/04/25 17:31:27;
  binding state free;
  hardware ethernet LL:KK:JJ:HH:DD:SS;
  uid "SDS";
    vendor-class "test1";
  set vendor-class-identifier = "test1";
  client-hostname "MNOP";
}
lease 000.000.00.002 {
  starts 3 2022/04/27 14:41:30;
  ends 3 2022/05/04 14:41:30;
  cltt 3 2022/04/27 14:41:30;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet MM:NN:DD:SS:RR:WW;
  uid "SASa";
  client-hostname "ADBC";
}
lease 000.001.00.003 {
  starts 3 2022/04/20 18:22:44;
  ends 5 2022/05/20 18:22:44;
  cltt 3 2022/04/20 18:22:44;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet AA:BB:CC:DD:FF:FF;
  uid "dasdad";
    vendor-class "test 3 vendor";
  set vendor-class-identifier = "test3";
}

Here In the second lease I am not having vendor-class filed which I am using in regex, so instead of skipping the vendor-class filed its trying to match the same in third lease, because of that I am getting only 2 match groups instead of 3 match groups. its merging 2nd and 3rd lease as one lease. Please help me out.
Please find the below link for better understanding:
https://regex101.com/r/dFCWk1/1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use

(?mis)^lease\s+(\S+)(?:(?!^lease\s).)*?binding state\s+(active|inactive|offline|free|abandoned?)(?:(?!^lease\s).)*?(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})(?:(?:(?!^lease\s).)*?vendor-class\s*(\"[^\"]+\").*?\s*(\"[^\"]+\");)?[^}]*

See the regex demo

Details:

  • Replace all .*? but last with the tempered greedy token, (?:(?!^lease\s).)*?, to avoid the section overflow during a match
  • Make sure the vendor matching pattern part is optional, see the (?:(?:(?!^lease\s).)*?vendor-class\s*(\"[^\"]+\").*?\s*(\"[^\"]+\");)? pattern, where the (?:(?!^lease\s).)*?vendor-class\s*(\"[^\"]+\").*?\s*(\"[^\"]+\"); is wrapped with the optional non-capturing group (see How do I make part of a regex match optional?)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading