Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: grimace: a fluent regular expression generator in Python

Reply
Thread Tools

Re: grimace: a fluent regular expression generator in Python

 
 
Joshua Landau
Guest
Posts: n/a
 
      07-16-2013
On 15 July 2013 23:21, Ben Last <(E-Mail Removed)> wrote:
> 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.l iteral(')')
> .then.one.literal("-").then.exactly(3).digits
> .then.one.dash.followed_by.exactly(4).digits.then. end
> .as_string()
> )


This looks really busy. How about something more like:

from grimace import RE, start, digits, dash, end
RE(start, "(", digits[3], ")-", digits[3], dash, digits[4], end).as_string()

?

and then you can do cool stuff like (Regex completely untested, I
hardly use the things):

RE((start | tab), (digits[:], inverse(dash)[4])[:2]) →
r"(^|\t)(\d*[^\-]{4,4}){0,2}"


> 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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grimace: a fluent regular expression generator in Python Ben Last Python 0 07-15-2013 10:21 PM
jFluent - a new library that provides a fluent interface over Javacore packages shinya Java 1 04-21-2008 12:59 PM
subtle side effect of generator/generator expression bonono@gmail.com Python 9 10-16-2005 06:42 PM
How long to learn C++ if fluent in Java? Rhino C++ 30 12-12-2004 10:58 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments