I think PodMaster is pointing you in the right direction.
This is a textbook example of needing to write a "little language" and using a parser for validation.
In order to use the modules he suggests you will need to know how to write a grammar. Hence, are going to need to get a hangle on BNF (Backus-Naur Form). I would suggest you do a google search on "bnf tutorials". You might have some luck there. You can also look at compilers and format automata resources.