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


Groups > comp.lang.python > #50728 > unrolled thread

grimace: a fluent regular expression generator in Python

Started byBen Last <benlast@gmail.com>
First post2013-07-16 06:21 +0800
Last post2013-07-16 06:21 +0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  grimace: a fluent regular expression generator in Python Ben Last <benlast@gmail.com> - 2013-07-16 06:21 +0800

#50728 — grimace: a fluent regular expression generator in Python

FromBen Last <benlast@gmail.com>
Date2013-07-16 06:21 +0800
Subjectgrimace: a fluent regular expression generator in Python
Message-ID<mailman.4758.1373960601.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hi all

I'd be interested in comments on a fluent regular expression generator I've
been playing with (inspired by the frustrations of a friend of mine who's
learning).

The general use case is to be able to construct RE strings such as:

r'^\(\d{3,3}\)-{1,1}\d{3,3}\-{1,1}\d{4,4}$' (intended to match North
American phone number)

as:

from grimace import RE
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())

The intent is to provide clarity: since the strings would normally be
generated and compiled when a module is first imported, there's minimal
overhead.

It's on github at https://github.com/benlast/grimace and the first blog
post that explains it is here: http://benlast.livejournal.com/30871.html (I've
added to it since then).

Tests are the best explanation, and they're in the __init__ at the end so
that they're self-executing if the init is executed.

I'm thinking about other features to implement, and more use cases would be
welcome.

Cheers
ben

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web