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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.039 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'subject:Python': 0.06; 'init': 0.07; 'welcome.': 0.07; '__init__': 0.09; 'url:github': 0.09; 'cc:addr:python-list': 0.11; 'cheers': 0.12; 'executed.': 0.16; 'fluent': 0.16; 'subject:expression': 0.16; 'subject:generator': 0.16; 'subject:regular': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'module': 0.19; 'normally': 0.19; 'import': 0.22; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'number)': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'compiled': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'cool': 0.30; 'start,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'comments': 0.31; 'stuff': 0.32; 'regular': 0.32; 'url:python': 0.33; 'cases': 0.33; 'minimal': 0.33; "i'd": 0.34; 'something': 0.35; "who's": 0.35; 'received:google.com': 0.35; 'really': 0.36; 'explains': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'ben': 0.38; 'mine': 0.38; 'url:mail': 0.40; 'how': 0.40; 'expression': 0.60; 'first': 0.61; 'here:': 0.62; 'such': 0.63; 'july': 0.63; 'provide': 0.64; 'more': 0.64; 'north': 0.65; 'phone': 0.66; 'american': 0.66; 'busy.': 0.68; 'skip:r 40': 0.68; 'friend': 0.79; 'as:': 0.81; 'hardly': 0.84; 'skip:. 50': 0.84; 'skip:. 60': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=xB2TdA12DYvBydNlMWoSCpH93NxRPoRVve2gVsSMnag=; b=Jnf7FSbohYAyj0ub6dZNne2U4YffAGZuzDUd2JjsvNzq4Fh7DEpK8nPuHHYMSEy7k2 s8EsmczA2z88bAdXr++q6z6dog//u2+5STKNM/v8jMWMxnSY52eCLKrNCprqSHHpTt0+ PPlzw/5I7w6OMKnh9cIdDEoLsbq3eJnymBgEutQkeqI6Urf3t2myhAkrZi+4Hp5rvuce u1aBRigCVCgimIZhGanv9SL5d8TKzXE0ALBt29xLufyXY5nZCZ1mG8GzGzDMJUqsOjBA Ui0L7cOwhT9Gl3c40H5etaNZU2PlHILf1kzX4pRj+vPj/2/rkQtbt+/koWyrunRXAxHz GrUQ== X-Received: by 10.152.20.40 with SMTP id k8mr245859lae.25.1373963946648; Tue, 16 Jul 2013 01:39:06 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: References: From: Joshua Landau Date: Tue, 16 Jul 2013 09:38:26 +0100 X-Google-Sender-Auth: EC-z-dhXWAvSx_W7LjAeWdKy3sA Subject: Re: grimace: a fluent regular expression generator in Python To: ben@benlast.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373963948 news.xs4all.nl 15957 [2001:888:2000:d::a6]:60067 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50734 On 15 July 2013 23:21, Ben Last 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 =3D ( > 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() > ) 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]) =E2=86=92 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 p= ost > 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 >