parsing - PHP Make an Array from a Text Pattern (cfg file) -


so have couple of nagios cfg files, services,hosts,contacts,etc

i want parse these cfg files php handle data.

contactgroups.cfg

define contactgroup {      contactgroup_name       vap3      alias                   vap3_pre      members                 userz, userw }  define contactgroup {      contactgroup_name       vap4      alias                   vap4_push      members                 userx, usery } 

services.cfg

define service {     host_name                       hosta     service_description             hosta_hd     contact_groups                  vap2,vap3 }  define service {     host_name                       hostb     service_description             hostb_hd     contact_groups                  vap3,vap4 } 

so want parse like:

contactgroup_name[0] = "vap3"; alias[0] = "vap3_pre"; members [0] = "userz,userw";  contactgroup_name[1] = "vap4"; alias[1] = "vap4_push"; members [1] = "userx, usery"; 

and services file:

host_name [0] = "hosta"; service_description [0] = "hosta_hd"; contact_groups [0] = "vap2,vap3";  host_name [1] = "hostb"; service_description [1] = "hostb_hd"; contact_groups [1] = "vap3,vap4"; 

so can handle in php script arrays, these example of cfg files, contains more these 3 definitions...maybe regex or preg_match...?

in every half-complex language (in case config), parsing via regular expressions match more 1 token has edge cases fail.

assume take naive approach , parse object definition with

/define\s+([^\s]+){([^}]*)}/ 

this match every contactgroup or service example provided , put type in \1 , contents in \2. however, if there # in front, have filter out. should cycle on lines , remove comment lines, before start matching object definitions.

after notice, there more comments allowed, ; comment, filter out too. don't know allowed chars, if value somewhere may validly contain } you're in trouble.

if that's not case, use first regexp provided, split content lines , preg_split every line inside object definition with

/^\s*([^\s]+)\s+(.*)$/ 

where key in \1 , value in \2, value should trimed.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -