From: "David A. Black" Date: 2009-10-06T02:21:53+09:00 Subject: Re: regex simplifier? Hi -- On Tue, 6 Oct 2009, Roger Pack wrote: >> I tried to cook something up in the past. You can find it when >> searching the archives for subject "Alternate Regular Expressions?". >> HTH > > Very interesting thread. Did anything come of it? (or florian gross' > Regexp::English looks nice [2]-- Florian?) > > I like this syntax (this example matches things like "-2.718 + 3.14i"): > > PAT.float['re'] + REP0.whitespace + ALT("+", "-")['op'] + > REPO.whitespace + PAT.float['im'] + 'i' [1] > > I'm not sure how to do nested matches or optionals or what not however. > > I suppose that's the equivalent of (in 1.9) > float = /[-+]?\d+\.\d+/ > whitespace = /\s+/ > > %r{(?#{float})#{whitespace}(?[+-])#{whitespace}(?#{float})i} > > But the kicker is still how to remember that \s is white space, not \w. > And we've been forced to do some tip toeing around the complexities of > regex in order to make it readable. > > I had thought of a regex creator helper > > float = reg { optional(/[-+]/) + 'DIGIT+ \. DIGIT+' } > > The benefit being in not having to remember what \u or \g does, and not > having to remember what /(something)?/ means :) I'm going to put in a plug for learning regular expression syntax itself in a reasonably thorough way. I won't try to make a case about what's more readable, since that clearly depends on the reader, but I do strongly recommend that everyone take the time to become regex literate. The programming world in general is not going to convert to English-language-based regex wrappers (which, though some such projects are interesting, is a mercy, because such wrappers could easily start proliferating and competing with each other, turning the whole thing into yet another notation soup), so the only way to participate fully in the use of regular expressions is to be conversant with the actual notation. David -- David A. Black, Director Ruby Power and Light, LLC (http://www.rubypal.com) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2)