in reply to Re^3: Regex to match a Cisco ACL
in thread Regex to match a Cisco ACL

Amazing! So if I could write down my grammar accurately, bulk of the job is done. But for some reason it is not matching anything. Does it have to match all the tokens in order to have a successful match? I am suspicious of the grammar that I provided.

Any way here is the data you can test with (same as the one successfully used by CountZero for his test case):

access-list V420_IN extended permit object-group Symantec_Service_Grou +p 10.148.0.0 255.254.0.0 host 10.149.16.40 access-list V420_IN extended permit object-group Symantec_Service_Grou +p any any access-list V420_IN extended permit tcp any any range 137 139 access-list V420_IN extended permit tcp any any eq 445

I note that I didn't mention the possibility of 'any' for source & destination in my BNF grammar. It should be a simple addition though. But I wonder why even the first rule cannot be parsed. Here is the output with debug on

===================> Trying <grammar> from position 0 access-list V420_IN |...Trying <acl> | \FAIL <acl> \FAIL <grammar> ===================> Trying <grammar> from position 1 ccess-list V420_IN e |...Trying <acl> | \FAIL <acl> \FAIL <grammar> ===================> Trying <grammar> from position 2 cess-list V420_IN ex |...Trying <acl> | \FAIL <acl>
... and so on till the last line.

Replies are listed 'Best First'.
Re^5: Regex to match a Cisco ACL
by tchrist (Pilgrim) on May 22, 2011 at 16:19 UTC
    Try the updated version. It uses the test data. There are still grammar bugs, because the last 3 fail, although the first 2 succeed.

    I think you just need to get the grammar straight.

      Thanks a bunch! Your updated code is indeed amazing, for its level of detail and re-usable tokens. It just gives a greater level of control and granularity over matching schema without having to resort to a code that looks like hieroglyphics.

      In hindsight, it was silly of me to ask again without checking the grammar. The last 3 rules failing is no surprise since the grammar doesn't account for it yet. I should produce a complete code soon, for the sake of others looking for the same stuff.

      May I ask whether it is possible to code multi-line grammar also in to regex? For e.g the below section of config file

      object-group network NOC-NC-NC1 network-object 192.162.137.0 255.255.255.0 network-object 192.162.146.0 255.255.255.0 object-group network NOC-NC-NC2 network-object 192.162.131.0 255.255.255.0 network-object 192.162.134.0 255.255.255.0

      Should be read into a hash of arrays as:

      $object_hash{"NOC-NC-NC1"} => ["192.162.137.0 255.255.255.0","192.162. +146.0 255.255.255.0"] $object_hash{"NOC-NC-NC2"} => ["192.162.131.0 255.255.255.0","192.162. +134.0 255.255.255.0"]