Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95497

Re: Python re to extract useful information from each line

Newsgroups comp.lang.python
Date 2015-08-19 12:53 -0700
References <e5473ccc-4f7d-431d-93a7-1aeeededcbf0@googlegroups.com>
Message-ID <a459bee9-e3ed-4caf-a6dd-67823e818f3d@googlegroups.com> (permalink)
Subject Re: Python re to extract useful information from each line
From Paul McGuire <ptmcg@austin.rr.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python re to extract useful information from each line Kashif Rana <kashifrana84@gmail.com> - 2015-04-29 13:42 -0700
  Re: Python re to extract useful information from each line Kashif Rana <kashifrana84@gmail.com> - 2015-04-29 13:49 -0700
    Re: Python re to extract useful information from each line Emile van Sebille <emile@fenx.com> - 2015-04-29 14:22 -0700
    Re: Python re to extract useful information from each line MRAB <python@mrabarnett.plus.com> - 2015-04-29 22:28 +0100
    Re: Python re to extract useful information from each line Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 22:30 +0100
    Re: Python re to extract useful information from each line Tim Chase <python.list@tim.thechases.com> - 2015-04-29 17:38 -0500
  Re: Python re to extract useful information from each line sohcahtoa82@gmail.com - 2015-04-29 16:29 -0700
  Re: Python re to extract useful information from each line Paul McGuire <ptmcg@austin.rr.com> - 2015-08-19 12:53 -0700

csiph-web