regex - Regexp to find all subnet blocks in config file -



regex - Regexp to find all subnet blocks in config file -

i have such config file:

#dhcp server configuration file. deny unknown-clients; subnet 10.8.140.2 netmask 255.255.255.255 { host example{ optian:param; } host example2{ option2:param2; } } subnet 20.8.110.1 netmask 255.255.255.255 { }

and need find subnet blocks. problem subnet block can contain host blocks (with curly brackets). , cant build regexp match those.

so result should be: 1. subnet 10.8.140.2 netmask 255.255.255.255 { ... host {...} host{...}} 2. subnet 20.8.110.1 netmask 255.255.255.255 { ... }

you didn't named programming language. here comes illustration using recursive pattern in php (pcre):

class="lang-php prettyprint-override"><?php $conf = file_get_contents('/path/to/dhcp.conf'); # utilize recursive pattern, check link posted above $pattern = '/(subnet.*?)?\{((?>[^{}]+)|(?r))*\}/'; preg_match_all($pattern, $conf, $matches); foreach($matches[0] $match) { echo $match . php_eol . php_eol; }

php uses perl compatible regexes, can utilize same pattern in perl or other languages utilize same engine , back upwards recursion.

btw, syntax higlighter seems have fun pattern, not syntax error.

online example

regex

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -