Hi there, Is it possible to write a regex to parse Access rules from a Cisco config file. Right now I have a working code, built on a bunch of nested if's to parse it. But it is only as elegant as a tumbleweed and I want to know whether it can be improved upon.

The basic format is as below (format specific to access rules in a ASA firewall)

access-list $acl_name extended $action $protocol $source_network $dest_network $ports .*

The challenging part is that some sections of the rule can have one, two or three words as below:

$protocols - (ip|tcp|udp) (1 word) object-group <protocol object-group name> (2 word) $source_network - host <host name> (2 words) <ip address> <wildcard mask> (2 words) object-group <network object-group name> (2 words) any (1 word) $dest_network - host <host name> (2 words) <ip address> <wildcard mask> (2 words) object-group <network object-group name> (2 words) any (1 word) $port - eq <port_no> (2 words) - range <low_port> <high_port> (3 words) - <blank> (0 word)

So, there are basically 2 x 2 x 2 x 3 = 24 possible formations. Can a regex with conditional matching and other advanced wizardry to tackle this particular case? I am not looking for 24 different patterns to match separately, it is only slightly better when compared to nested if code. Once I get a solution and hopefully understand it, I hope I can extended it to other form of ACLs (perhaps include Juniper devices also)


In reply to Regex to match a Cisco ACL by Anonymous Monk

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.