From: Bill Kelly Date: 2001-11-22T04:45:36+09:00 Subject: [ruby-talk:26064] Re: ANN: regex engine development (was: Re: Why not xmlparser?) From: "David Alan Black" > Hello -- Hi :) > On Thu, 22 Nov 2001, Bill Kelly wrote: > [...] > > That /foo (*followed-by* bar)/rx is starting to look pretty nice I > > must say. > > Funny, I find it almost unreadable. Same with named captured > substrings, etc. To me it's sort of like starting a piece of music > with: > > (*the-note-below-middle-C*) Fair enough, I think I was getting focused on the local aesthetics (like does this span of characters appear pleasing to the eye) and suspending judgment on whether I myself might want to read a regexp written using that syntax. But also, my interest was particularly piqued when Robert Feldt the other day [ruby-talk:25887] posted: non_paren = plus( any_but( "(", ")" ) ) re = seq("(", (non_paren || recurse), ")") as the Rockit equivalent of: re = %r{ \( (?: (?> [^()]+ ) # Non-parens without backtracking | (??{ re }) # (Sub-)group with matching parens )* \) }x Which I think fares pretty well against the regexp. And so, although I can read the regexp fine, I began to wonder whether there might be some syntax extension out there that would allow easy naming of subexpressions or something. . . Of course, perhaps the Simplest Thing in this case is just to composite the regexp: (requires the redefinition of #to_s in Regexp class) non_paren = /[^()]/ re = %r{ \( (?: (?>#{non_paren}) | (??{re}) )* \) }x (Although I suspect I would have just written non_paren inline anyway.... since [^()] says it pretty clearly.) Also, I have found named capture groups useful in Python, not to make the regexp clearer, but to reduce the coupling between the regexp itself and the code using the regexp wanting to pull out matched groups. > instead of > > 0 > -|---- > -|---- > ------ > ------ > ------ > > (That's bass clef, by the way :-) > > I guess over the years I've trained my brain to parse regular > expressions in such a way that having them described in English is > actually a fairly major impediment to my understanding them. I was thinking of having my middle name changed to (?i-x:W(i)l{2}\1am) > I have no axe to grind about this (as long as it doesn't catch on :-) > Just noting in passing that people have different takes on it. > > > David Regards, Bill