Python re to extract useful information from each line

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Thu Apr 30 01:29:57 CEST 2015


On Wednesday, April 29, 2015 at 1:42:18 PM UTC-7, Kashif Rana wrote:
> Hello Experts
> 
> I have below lines with some variations.
> 
> 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
> 
> Please help me to write the regular expression to extract below information in parenthesis, if exist from each line. Please note that some items may exist or not like nat or log
> 
> set policy id (id) from (from) to (to) (source) (destination) (service) nat (src or dst) (dip-id 4) or (ip 10.10.10.10) port (dst-port) (action) schedule (schedule) (log)

If you don't have to worry about spaces in your strings, I'd just use split().  If you DO need to worry about spaces, it'd be trivial to write your own parser that stepped through the string a single character at a time.  The shlex module does this, but might not work for you.  I don't know how it would handle an IP address.



More information about the Python-list mailing list