From: Saynatkari Date: 2005-04-03T23:16:13+09:00 Subject: Re: Iterating through a string and removing leading characters Le 3/4/2005, "Randy Kramer" a �crit: >On Sunday 03 April 2005 09:36 am, Saynatkari wrote: >> Le 3/4/2005, "Mathieu Bouchard" a �crit: >> >However if I were to solve the problem of finding which sub-regexp has >> >been matched in (A|B|C|...), I'd edit re.c and add a (?)-feature for >> >filling a $-slot with a value that doesn't come from the string, e.g. >> > >> >/((?"Aah"(A))|(?"Bay"(B))|(?"Say"(C))|(?"Day"(D)))/ >> > >> >Would put one of "Aah", "Bay", "Say", "Day" strings in $2... >> > >> >But this doesn't make sense yet, as one would expect it to instead be put >> >in one of $2, $4, $6, $8, ... to be consistent with current regexp >> >semantics; and looking up possibly all of those looking for a nonnil >> >$-slot is a O(n)-time thing. There ought to be a better way, that is, >> >something both fast and consistent with current semantics, but I can't >> >think of any as of now. Do you have any ideas? If you have something good >> >then I think it should be a RCR. > >I like this idea (I think it would be helpful with my problem of fast parsing >of TWiki markup). (And, if I decide to do a character by character thing >myself in c, it sounds like re.c would be something to study.) > >But, instead of (or maybe in addition to) returning the "Aah", "Bay" in $2, $4 >or whatever, how about returning an integer (somehow) that indicates which >one matched? I mentioned this a while ago; I edited my strscan.c to provide methods #matched_groups and #first_matched_group for accessing this information (I used it to dispatch a block to process a particular type of match). I can clean it up and post a patch somewhere if it seems a useful feature for other people. >As a Ruby newbie, I'm not sure that's all I'd be looking for--if Ruby gives me >a way to access those strings directly by the integer, that would be great. >For example, say the integer is returned as $a (to avoid collision with $1, >$2 ...). I'd like to be able to access the "Aah", "Bay, ... by something >like $($a)--barring that, I can simply maintain a separate array of the >"Aah"... and access that array using $a. > >Randy Kramer > >> >> This is a worthy idea, certainly! I should not expect it to cause >> any confusion so long as the notation is standardised, particularly >> through the standard ? extension switch. Perhaps the inner braces >> would not be allowed for clarity? Unfortunately the rubyish ?! >> (in-place method) and ?# (string interpolation) are already taken :) >> >> /(?-> 'match' 'replacement')/ E No-one expects the Solaris POSIX implementation!