Here is a first shot at a pyparsing parser for these lines: from pyparsing import * SET,POLICY,ID,FROM,TO,NAT,SRC,DST,IP,PORT,SCHEDULE,LOG,PERMIT,ALLOW,DENY = map(CaselessKeyword, "SET,POLICY,ID,FROM,TO,NAT,SRC,DST,IP,PORT,SCHEDULE,LOG,PERMIT,ALLOW,DENY".split(','))
integer = Word(nums) ipAddr = Combine(integer + ('.'+integer)*3) quotedString.setParseAction(removeQuotes) logParser = (SET + POLICY + ID + integer("id") + FROM + quotedString("from_") + TO + quotedString("to_") + quotedString("service")) I run this with: for line in """ 1- set policy id 1000 from "Untrust" to "Trust" "Any" "1.1.1.1" "HTTP" nat dst ip 10.10.10.10 port 8000 permit log 2- set policy id 5000 from "Trust" to "Untrust" "Any" "microsoft.com" "HTTP" nat src permit schedule "14August2014" log 3- set policy id 7000 from "Trust" to "Untrust" "Users" "Any" "ANY" nat src dip-id 4 permit log 4- set policy id 7000 from "Trust" to "Untrust" "servers" "Any" "ANY" deny """.splitlines(): line = line.strip() if not line: continue print (integer + '-' + logParser).parseString(line).dump() print Getting: ['1', '-', 'SET', 'POLICY', 'ID', '1000', 'FROM', 'Untrust', 'TO', 'Trust', 'Any'] - from_: Untrust - id: 1000 - service: Any - to_: Trust ['2', '-', 'SET', 'POLICY', 'ID', '5000', 'FROM', 'Trust', 'TO', 'Untrust', 'Any'] - from_: Trust - id: 5000 - service: Any - to_: Untrust ['3', '-', 'SET', 'POLICY', 'ID', '7000', 'FROM', 'Trust', 'TO', 'Untrust', 'Users'] - from_: Trust - id: 7000 - service: Users - to_: Untrust ['4', '-', 'SET', 'POLICY', 'ID', '7000', 'FROM', 'Trust', 'TO', 'Untrust', 'servers'] - from_: Trust - id: 7000 - service: servers - to_: Untrust Pyparsing adds Optional classes so that you can include expressions for pieces that might be missing like "... + Optional(NAT + (SRC | DST)) + ..." -- Paul -- https://mail.python.org/mailman/listinfo/python-list