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


Groups > comp.lang.python > #50743

Re: grimace: a fluent regular expression generator in Python

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <2013@jmunch.dk>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'languages.': 0.04; 'syntax': 0.04; 'subject:Python': 0.06; 'nested': 0.07; '"("': 0.09; 'anders': 0.09; 'default.': 0.09; 'cc:addr:python-list': 0.11; '""")': 0.16; '")"': 0.16; '"-"': 0.16; 'cool.': 0.16; 'ignoring': 0.16; 'irregular': 0.16; 'lexical': 0.16; 'literals': 0.16; 'notation': 0.16; 'readable': 0.16; 'subject:expression': 0.16; 'subject:generator': 0.16; 'subject:regular': 0.16; 'terse': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'putting': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; "shouldn't": 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; "i'm": 0.30; 'quotes': 0.31; 'received:dk': 0.31; 'could': 0.34; 'problem': 0.35; 'really': 0.36; 'too': 0.37; 'two': 0.37; 'ben': 0.38; 'bad': 0.39; 'embedded': 0.39; 'skip:. 10': 0.39; 'structure': 0.39; 'sure': 0.39; 'how': 0.40; 'miss': 0.74; 'improvement,': 0.84; 'regexp': 0.84; 'skip:. 50': 0.84; 'skip:. 60': 0.84; 'write:': 0.91
Date Tue, 16 Jul 2013 13:38:35 +0200
From "Anders J. Munch" <2013@jmunch.dk>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
MIME-Version 1.0
To ben@benlast.com
Subject Re: grimace: a fluent regular expression generator in Python
References <CAMCkecpUOoCQ5pTpyZ_+UQF12hAfvMx5sZB1zziQea-bv86L0A@mail.gmail.com>
In-Reply-To <CAMCkecpUOoCQ5pTpyZ_+UQF12hAfvMx5sZB1zziQea-bv86L0A@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Mailman-Approved-At Tue, 16 Jul 2013 14:48:50 +0200
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4772.1373978931.3114.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373978931 news.xs4all.nl 15913 [2001:888:2000:d::a6]:49173
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50743

Show key headers only | View raw


Ben Last wrote:
> north_american_number_re = (RE().start
>
> .literal('(').followed_by.exactly(3).digits.then.literal(')')
>                                      .then.one.literal("-").then.exactly(3).digits
>
> .then.one.dash.followed_by.exactly(4).digits.then.end
>                                      .as_string())

Very cool.  It's a bit verbose for my taste, and I'm not sure how well it will 
cope with nested structure.

Here's my take on what readable regexps could look like:

north_american_number_re = RE.compile(r"""
     ^
     "(" digit{3} ")"  # And why shouldn't a regexp
     "-" digit{3}      # include en embedded comment?
     "-" digit{4}
     $
""")

The problem with Perl-style regexp notation isn't so much that it's terse - it's 
that the syntax is irregular (sic) and doesn't follow modern principles for 
lexical structure in computer languages.  You can get a long way just by 
ignoring whitespace, putting literals in quotes and allowing embedded comments.

Setting the re.VERBOSE flag achieves two out of three, so you can write:

north_american_number_re = RE.compile(r"""
     ^
     ( \d{3} )   # Definite improvement, though I really miss putting
     - \d{3}     # literals in quotes.
     - \d{4}
     $
""")

It's too bad re.VERBOSE isn't the default.

regards, Anders

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


Thread

Re: grimace: a fluent regular expression generator in Python "Anders J. Munch" <2013@jmunch.dk> - 2013-07-16 13:38 +0200
  Re: grimace: a fluent regular expression generator in Python Roy Smith <roy@panix.com> - 2013-07-17 10:22 -0400

csiph-web