Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89586
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'parser': 0.07; 'line:': 0.09; 'skip:\\ 10': 0.09; 'cc:addr :python-list': 0.11; '-tkc': 0.16; 'expression,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'which,': 0.16; 'component': 0.16; 'wrote:': 0.18; 'appears': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'first,': 0.26; 'switch': 0.26; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'said,': 0.30; 'comments': 0.31; 'regular': 0.32; 'raw': 0.33; 'actual': 0.34; 'subject:from': 0.34; 'charset:us-ascii': 0.36; 'received:10': 0.37; 'pm,': 0.38; 'name': 0.63; 'trouble.': 0.91 |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-MC-Relay | Neutral |
| X-MailChannels-SenderId | wwwh|x-authuser|tim@thechases.com |
| X-MailChannels-Auth-Id | wwwh |
| X-MC-Loop-Signature | 1430347089737:3269609732 |
| X-MC-Ingress-Time | 1430347089736 |
| Date | Wed, 29 Apr 2015 17:38:57 -0500 |
| From | Tim Chase <python.list@tim.thechases.com> |
| To | Emile van Sebille <emile@fenx.com> |
| Cc | python-list@python.org |
| Subject | Re: Python re to extract useful information from each line |
| In-Reply-To | <mhri39$n45$1@ger.gmane.org> |
| References | <e5473ccc-4f7d-431d-93a7-1aeeededcbf0@googlegroups.com> <220dafbc-25f0-48a7-b37a-c8a77a6f2ffa@googlegroups.com> <mhri39$n45$1@ger.gmane.org> |
| X-Mailer | Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-AuthUser | tim@thechases.com |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.105.1430349368.3680.python-list@python.org> (permalink) |
| Lines | 80 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1430349368 news.xs4all.nl 2849 [2001:888:2000:d::a6]:53650 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:89586 |
Show key headers only | View raw
On 2015-04-29 14:22, Emile van Sebille wrote:
> On 4/29/2015 1:49 PM, Kashif Rana wrote:
> > 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)?$'
> > )
>
> ... and that's why we avoid regular expressions... it makes my head
> hurt just looking at that line noise.
First, it appears the OP isn't using raw strings which make those
back-slashes just ask for trouble.
That said, it would be a lot better if the OP made use of re.VERBOSE
to put each component on its own line:
pol_elements = re.compile(r"""
id
\s
(?P<p_id>.+?)
(?:
\s
name
\s
(?P<p_name>.+?)
)?
\s
from
\s
(?P<p_from>.+?)
\s
to
\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+
)?
(
\s
ip
\s
(?P<p_nat_dst_ip>[\d\.]+)
\s
port
(?P<dst_nat_port>\d+)
)?
)?
\s
(?P<p_action>[^\s]+?)
(?:
\s
schedule
\s
(?P<p_schedule>[^\s]+?)
)?
(?P<p_log_status>\slog)?
$
""", re.VERBOSE)
which, with some copious comments in the expression, would make it
almost readable.
Alternatively, switch to an actual parser like pyparsing.
-tkc
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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