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


Groups > comp.lang.python > #89574

Re: Python re to extract useful information from each line

X-Received by 10.182.16.163 with SMTP id h3mr1547338obd.14.1430340545205; Wed, 29 Apr 2015 13:49:05 -0700 (PDT)
X-Received by 10.140.100.136 with SMTP id s8mr21313qge.2.1430340545180; Wed, 29 Apr 2015 13:49:05 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!l13no11803916iga.0!news-out.google.com!k20ni1154qgd.0!nntp.google.com!j5no4390551qga.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Wed, 29 Apr 2015 13:49:05 -0700 (PDT)
In-Reply-To <e5473ccc-4f7d-431d-93a7-1aeeededcbf0@googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=86.97.221.214; posting-account=fQsJiwoAAADViUtOwqLUMmQvdel93bSP
NNTP-Posting-Host 86.97.221.214
References <e5473ccc-4f7d-431d-93a7-1aeeededcbf0@googlegroups.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <220dafbc-25f0-48a7-b37a-c8a77a6f2ffa@googlegroups.com> (permalink)
Subject Re: Python re to extract useful information from each line
From Kashif Rana <kashifrana84@gmail.com>
Injection-Date Wed, 29 Apr 2015 20:49:05 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.python:89574

Show key headers only | View raw


On Thursday, April 30, 2015 at 12:42:18 AM UTC+4, 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)

I tried below re and its not working.

id\s(?P<p_id>.+?)(?:\sname\s(?P<p_name>.+?))?\sfrom\s(?P<p_from>.+?)\sto\s(?P<p_to>.+?)\s{2}(?P<p_src>[^\s]+?)\s(?P<p_dst>[^\s]+?)\s(?P<p_port>[^\s]+?)(?:\s(?P<p_nat_status>nat)\s(?P<p_nat_type>\w+)(\s?P<p_nat_src_ip>dip-id\s\d+)?(\sip\s(?P<p_nat_dst_ip>[\d\.]+)\sport(?P<dst_nat_port>\d+))?)?\s(?P<p_action>[^\s]+?)(?:\sschedule\s(?P<p_schedule>[^\s]+?))?(?P<p_log_status>\slog)?$

If I ignore the line 1. I made below re and its working and giving me all info.

pol_elements = re.compile('id\s(?P<p_id>.+?)(?:\sname\s(?P<p_name>.+?))?\sfrom\s(?P<p_from>.+?)\sto\s(?P<p_to>.+?)\s{2}(?P<p_src>[^\s]+?)\s(?P<p_dst>[^\s]+?)\s(?P<p_port>[^\s]+?)(?:(?P<p_nat_status>\snat)\s(?P<p_nat_type>[^\s]+?)(?P<p_nat_ip>\sdip-id\s[^\s]+?)?)?\s(?P<p_action>[^\s]+?)(?:\sschedule\s(?P<p_schedule>[^\s]+?))?(?P<p_log_status>\slog)?$'
)

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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